Class BidiUtils


  • public class BidiUtils
    extends java.lang.Object
    Utility functions for performing common Bidi tests on strings.
    • Method Detail

      • get

        public static BidiUtils get()
        Get an instance of BidiUtils.
        Returns:
        An instance of BidiUtils
      • endsWithLtr

        public boolean endsWithLtr​(java.lang.String str)
        Like endsWithLtr(String, boolean), but assumes str is not HTML / HTML-escaped.
      • endsWithLtr

        public boolean endsWithLtr​(java.lang.String str,
                                   boolean isHtml)
        Check whether the last strongly-directional character in the string is LTR.
        Parameters:
        str - the string to check
        isHtml - whether str is HTML / HTML-escaped
        Returns:
        whether LTR exit directionality was detected
      • endsWithRtl

        public boolean endsWithRtl​(java.lang.String str)
        Like endsWithRtl(String, boolean), but assumes str is not HTML / HTML-escaped.
      • endsWithRtl

        public boolean endsWithRtl​(java.lang.String str,
                                   boolean isHtml)
        Check whether the last strongly-directional character in the string is RTL.
        Parameters:
        str - the string to check
        isHtml - whether str is HTML / HTML-escaped
        Returns:
        whether RTL exit directionality was detected
      • estimateDirection

        public HasDirection.Direction estimateDirection​(java.lang.String str,
                                                        boolean isHtml)
        Estimates the directionality of a string based on relative word counts. If the number of RTL words is above a certain percentage of the total number of strongly directional words, returns RTL. Otherwise, if any words are strongly or weakly LTR, returns LTR. Otherwise, returns DEFAULT, which is used to mean "neutral". Numbers are counted as weakly LTR.
        Parameters:
        str - the string to check
        isHtml - whether str is HTML / HTML-escaped. Use this to ignore HTML tags and escapes that would otherwise be mistaken for LTR text.
        Returns:
        the string's directionality
      • hasAnyLtr

        public boolean hasAnyLtr​(java.lang.String str)
        Like hasAnyLtr(String, boolean), but assumes str is not HTML / HTML-escaped.
        Parameters:
        str - the string to be tested
        Returns:
        whether the string contains any LTR characters
      • hasAnyLtr

        public boolean hasAnyLtr​(java.lang.String str,
                                 boolean isHtml)
        Checks if the given string has any LTR characters in it.
        Parameters:
        str - the string to be tested
        isHtml - whether str is HTML / HTML-escaped
        Returns:
        whether the string contains any LTR characters
      • hasAnyRtl

        public boolean hasAnyRtl​(java.lang.String str)
        Like hasAnyRtl(String, boolean), but assumes str is not HTML / HTML-escaped.
        Parameters:
        str - the string to be tested
        Returns:
        whether the string contains any RTL characters
      • hasAnyRtl

        public boolean hasAnyRtl​(java.lang.String str,
                                 boolean isHtml)
        Checks if the given string has any RTL characters in it.
        Parameters:
        isHtml - whether str is HTML / HTML-escaped
        str - the string to be tested
        Returns:
        whether the string contains any RTL characters
      • startsWithLtr

        public boolean startsWithLtr​(java.lang.String str)
        Like startsWithLtr(String, boolean), but assumes str is not HTML / HTML-escaped.
      • startsWithLtr

        public boolean startsWithLtr​(java.lang.String str,
                                     boolean isHtml)
        Check whether the first strongly-directional character in the string is LTR.
        Parameters:
        str - the string to check
        isHtml - whether str is HTML / HTML-escaped
        Returns:
        whether LTR exit directionality was detected
      • startsWithRtl

        public boolean startsWithRtl​(java.lang.String str)
        Like startsWithRtl(String, boolean), but assumes str is not HTML / HTML-escaped.
      • startsWithRtl

        public boolean startsWithRtl​(java.lang.String str,
                                     boolean isHtml)
        Check whether the first strongly-directional character in the string is RTL.
        Parameters:
        str - the string to check
        isHtml - whether str is HTML / HTML-escaped
        Returns:
        whether RTL exit directionality was detected
      • stripHtmlIfNeeded

        java.lang.String stripHtmlIfNeeded​(java.lang.String str,
                                           boolean isStripNeeded)
        Returns the input text with spaces instead of HTML tags or HTML escapes, if isStripNeeded is true. Else returns the input as is. Useful for text directionality estimation. Note: the function should not be used in other contexts; it is not 100% correct, but rather a good-enough implementation for directionality estimation purposes.