Class BidiUtils

java.lang.Object
com.google.gwt.i18n.shared.BidiUtils

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

    • get

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

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

      public boolean endsWithLtr(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(String str)
      Like endsWithRtl(String, boolean), but assumes str is not HTML / HTML-escaped.
    • endsWithRtl

      public boolean endsWithRtl(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(String str)
      Like estimateDirection(String, boolean), but assumes str is not HTML / HTML-escaped.
    • estimateDirection

      public HasDirection.Direction estimateDirection(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(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(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(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(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(String str)
      Like startsWithLtr(String, boolean), but assumes str is not HTML / HTML-escaped.
    • startsWithLtr

      public boolean startsWithLtr(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(String str)
      Like startsWithRtl(String, boolean), but assumes str is not HTML / HTML-escaped.
    • startsWithRtl

      public boolean startsWithRtl(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

      String stripHtmlIfNeeded(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.