Java Code Examples for java.text.CharacterIterator#previous()

The following examples show how to use java.text.CharacterIterator#previous() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CharacterIteration.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public static int current32(CharacterIterator ci) {
    char  lead   = ci.current();
    int   retVal = lead;
    if (retVal < UTF16.LEAD_SURROGATE_MIN_VALUE) {
        return retVal;   
    }
    if (UTF16.isLeadSurrogate(lead)) {
        int  trail = (int)ci.next();
        ci.previous();
        if (UTF16.isTrailSurrogate((char)trail)) {
            retVal = ((lead  - UTF16.LEAD_SURROGATE_MIN_VALUE) << 10) +
                     (trail - UTF16.TRAIL_SURROGATE_MIN_VALUE) +
                     UTF16.SUPPLEMENTARY_MIN_VALUE;
        }
     } else {
        if (lead == CharacterIterator.DONE) {
            if (ci.getIndex() >= ci.getEndIndex())   {
                retVal = DONE32;   
            }
        }
     }
    return retVal;
}
 
Example 2
Source File: CharacterIteration.java    From fitnotifications with Apache License 2.0 6 votes vote down vote up
public static int previous32(CharacterIterator ci) {
    if (ci.getIndex() <= ci.getBeginIndex()) {
        return DONE32;   
    }
    char trail = ci.previous();
    int retVal = trail;
    if (UTF16.isTrailSurrogate(trail) && ci.getIndex()>ci.getBeginIndex()) {
        char lead = ci.previous();
        if (UTF16.isLeadSurrogate(lead)) {
            retVal = (((int)lead  - UTF16.LEAD_SURROGATE_MIN_VALUE) << 10) +
                      ((int)trail - UTF16.TRAIL_SURROGATE_MIN_VALUE) +
                      UTF16.SUPPLEMENTARY_MIN_VALUE;
        } else {
            ci.next();
        }           
    }
    return retVal;
}
 
Example 3
Source File: CharacterIteration.java    From fitnotifications with Apache License 2.0 6 votes vote down vote up
public static int current32(CharacterIterator ci) {
    char  lead   = ci.current();
    int   retVal = lead;
    if (retVal < UTF16.LEAD_SURROGATE_MIN_VALUE) {
        return retVal;   
    }
    if (UTF16.isLeadSurrogate(lead)) {
        int  trail = (int)ci.next();
        ci.previous();
        if (UTF16.isTrailSurrogate((char)trail)) {
            retVal = ((lead  - UTF16.LEAD_SURROGATE_MIN_VALUE) << 10) +
                     (trail - UTF16.TRAIL_SURROGATE_MIN_VALUE) +
                     UTF16.SUPPLEMENTARY_MIN_VALUE;
        }
     } else {
        if (lead == CharacterIterator.DONE) {
            if (ci.getIndex() >= ci.getEndIndex())   {
                retVal = DONE32;   
            }
        }
     }
    return retVal;
}
 
Example 4
Source File: CharacterIteration.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public static int previous32(CharacterIterator ci) {
    if (ci.getIndex() <= ci.getBeginIndex()) {
        return DONE32;   
    }
    char trail = ci.previous();
    int retVal = trail;
    if (UTF16.isTrailSurrogate(trail) && ci.getIndex()>ci.getBeginIndex()) {
        char lead = ci.previous();
        if (UTF16.isLeadSurrogate(lead)) {
            retVal = (((int)lead  - UTF16.LEAD_SURROGATE_MIN_VALUE) << 10) +
                      ((int)trail - UTF16.TRAIL_SURROGATE_MIN_VALUE) +
                      UTF16.SUPPLEMENTARY_MIN_VALUE;
        } else {
            ci.next();
        }           
    }
    return retVal;
}
 
Example 5
Source File: CharacterIteration.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public static int nextTrail32(CharacterIterator ci, int lead) {
    if (lead == CharacterIterator.DONE && ci.getIndex() >= ci.getEndIndex()) {
        return DONE32;
    }
    int retVal = lead;
    if (lead <= UTF16.LEAD_SURROGATE_MAX_VALUE) {
        char  cTrail = ci.next();
        if (UTF16.isTrailSurrogate(cTrail)) {
            retVal = ((lead  - UTF16.LEAD_SURROGATE_MIN_VALUE) << 10) +
                        (cTrail - UTF16.TRAIL_SURROGATE_MIN_VALUE) +
                        UTF16.SUPPLEMENTARY_MIN_VALUE;
        } else {
            ci.previous();
        }
    }
    return retVal;
}
 
Example 6
Source File: TextBoxFactory.java    From stendhal with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Check if a location is at a hard line break.
 *
 * @param cit iterator
 * @return <code>true</code> if there is a hard line break
 */
private boolean isHardLineBreak(final CharacterIterator cit) {
	// save the location while we are checking the preceding characters
	final int currentIndex = cit.getIndex();

	char currentChar = cit.previous();
	while (currentChar != CharacterIterator.DONE && !Character.isLetterOrDigit(currentChar)) {
		if (currentChar == '\n') {
			cit.setIndex(currentIndex);
			return true;
		}
		currentChar = cit.previous();
	}
	cit.setIndex(currentIndex);

	return false;
}
 
Example 7
Source File: Text.java    From RDFS with Apache License 2.0 5 votes vote down vote up
/**
 * For the given string, returns the number of UTF-8 bytes
 * required to encode the string.
 * @param string text to encode
 * @return number of UTF-8 bytes required to encode
 */
public static int utf8Length(String string) {
  CharacterIterator iter = new StringCharacterIterator(string);
  char ch = iter.first();
  int size = 0;
  while (ch != CharacterIterator.DONE) {
    if ((ch >= 0xD800) && (ch < 0xDC00)) {
      // surrogate pair?
      char trail = iter.next();
      if ((trail > 0xDBFF) && (trail < 0xE000)) {
        // valid pair
        size += 4;
      } else {
        // invalid pair
        size += 3;
        iter.previous(); // rewind one
      }
    } else if (ch < 0x80) {
      size++;
    } else if (ch < 0x800) {
      size += 2;
    } else {
      // ch < 0x10000, that is, the largest char value
      size += 3;
    }
    ch = iter.next();
  }
  return size;
}
 
Example 8
Source File: Text.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * For the given string, returns the number of UTF-8 bytes required to encode the string.
 *
 * @param string
 *          text to encode
 * @return number of UTF-8 bytes required to encode
 */
public static int utf8Length(String string) {
  CharacterIterator iter = new StringCharacterIterator(string);
  char ch = iter.first();
  int size = 0;
  while (ch != CharacterIterator.DONE) {
    if ((ch >= 0xD800) && (ch < 0xDC00)) {
      // surrogate pair?
      char trail = iter.next();
      if ((trail > 0xDBFF) && (trail < 0xE000)) {
        // valid pair
        size += 4;
      } else {
        // invalid pair
        size += 3;
        iter.previous(); // rewind one
      }
    } else if (ch < 0x80) {
      size++;
    } else if (ch < 0x800) {
      size += 2;
    } else {
      // ch < 0x10000, that is, the largest char value
      size += 3;
    }
    ch = iter.next();
  }
  return size;
}
 
Example 9
Source File: Text.java    From Canova with Apache License 2.0 5 votes vote down vote up
/**
 * For the given string, returns the number of UTF-8 bytes
 * required to encode the string.
 * @param string text to encode
 * @return number of UTF-8 bytes required to encode
 */
public static int utf8Length(String string) {
    CharacterIterator iter = new StringCharacterIterator(string);
    char ch = iter.first();
    int size = 0;
    while (ch != CharacterIterator.DONE) {
        if ((ch >= 0xD800) && (ch < 0xDC00)) {
            // surrogate pair?
            char trail = iter.next();
            if ((trail > 0xDBFF) && (trail < 0xE000)) {
                // valid pair
                size += 4;
            } else {
                // invalid pair
                size += 3;
                iter.previous(); // rewind one
            }
        } else if (ch < 0x80) {
            size++;
        } else if (ch < 0x800) {
            size += 2;
        } else {
            // ch < 0x10000, that is, the largest char value
            size += 3;
        }
        ch = iter.next();
    }
    return size;
}
 
Example 10
Source File: CharacterIteration.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
  * Move the iterator forward to the next code point, and return that code point,
  *   leaving the iterator positioned at char returned.
  *   For Supplementary chars, the iterator is left positioned at the lead surrogate.
  * @param ci  The character iterator
  * @return    The next code point.
  */
 public static int next32(CharacterIterator ci) {
     // If the current position is at a surrogate pair, move to the trail surrogate
     //   which leaves it in position for underlying iterator's next() to work.
     int c = ci.current();
     if (c >= UTF16.LEAD_SURROGATE_MIN_VALUE && c<=UTF16.LEAD_SURROGATE_MAX_VALUE) {
         c = ci.next();   
         if (c<UTF16.TRAIL_SURROGATE_MIN_VALUE || c>UTF16.TRAIL_SURROGATE_MAX_VALUE) {
             ci.previous();   
         }
     }

     // For BMP chars, this next() is the real deal.
     c = ci.next();
     
     // If we might have a lead surrogate, we need to peak ahead to get the trail 
     //  even though we don't want to really be positioned there.
     if (c >= UTF16.LEAD_SURROGATE_MIN_VALUE) {
         c = nextTrail32(ci, c);   
     }
     
     if (c >= UTF16.SUPPLEMENTARY_MIN_VALUE && c != DONE32) {
         // We got a supplementary char.  Back the iterator up to the postion
         // of the lead surrogate.
         ci.previous();   
     }
     return c;
}
 
Example 11
Source File: StringSearch.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private static int codePointBefore(CharacterIterator iter, int index) {
    int currentIterIndex = iter.getIndex();
    iter.setIndex(index);
    char codeUnit = iter.previous();
    int cp = codeUnit;
    if (Character.isLowSurrogate(codeUnit)) {
        char prevUnit = iter.previous();
        if (Character.isHighSurrogate(prevUnit)) {
            cp = Character.toCodePoint(prevUnit, codeUnit);
        }
    }
    iter.setIndex(currentIterIndex);  // restore iter position
    return cp;
}
 
Example 12
Source File: StringUtils.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public static String reverse(final String value) {
  if (value != null) {
    final CharacterIterator it = new StringCharacterIterator(value);
    final StringBuilder buffer = new StringBuilder();

    for (char c = it.last(); c != CharacterIterator.DONE; c = it.previous()) {
      buffer.append(c);
    }

    return buffer.toString();
  }

  return value;
}
 
Example 13
Source File: StringRecord.java    From stratosphere with Apache License 2.0 5 votes vote down vote up
/**
 * For the given string, returns the number of UTF-8 bytes required to
 * encode the string.
 * 
 * @param string
 *        text to encode
 * @return number of UTF-8 bytes required to encode
 */
public static int utf8Length(final String string) {
	final CharacterIterator iter = new StringCharacterIterator(string);
	char ch = iter.first();
	int size = 0;
	while (ch != CharacterIterator.DONE) {
		if ((ch >= 0xD800) && (ch < 0xDC00)) {
			// surrogate pair?
			char trail = iter.next();
			if ((trail > 0xDBFF) && (trail < 0xE000)) {
				// valid pair
				size += 4;
			} else {
				// invalid pair
				size += 3;
				iter.previous(); // rewind one
			}
		} else if (ch < 0x80) {
			size++;
		} else if (ch < 0x800) {
			size += 2;
		} else {
			// ch < 0x10000, that is, the largest char value
			size += 3;
		}
		ch = iter.next();
	}
	return size;
}
 
Example 14
Source File: Text.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * For the given string, returns the number of UTF-8 bytes
 * required to encode the string.
 * @param string text to encode
 * @return number of UTF-8 bytes required to encode
 */
public static int utf8Length(String string) {
  CharacterIterator iter = new StringCharacterIterator(string);
  char ch = iter.first();
  int size = 0;
  while (ch != CharacterIterator.DONE) {
    if ((ch >= 0xD800) && (ch < 0xDC00)) {
      // surrogate pair?
      char trail = iter.next();
      if ((trail > 0xDBFF) && (trail < 0xE000)) {
        // valid pair
        size += 4;
      } else {
        // invalid pair
        size += 3;
        iter.previous(); // rewind one
      }
    } else if (ch < 0x80) {
      size++;
    } else if (ch < 0x800) {
      size += 2;
    } else {
      // ch < 0x10000, that is, the largest char value
      size += 3;
    }
    ch = iter.next();
  }
  return size;
}
 
Example 15
Source File: StringUtils.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public static String reverse(final String value) {
  if (value != null) {
    final CharacterIterator it = new StringCharacterIterator(value);
    final StringBuilder buffer = new StringBuilder();

    for (char c = it.last(); c != CharacterIterator.DONE; c = it.previous()) {
      buffer.append(c);
    }

    return buffer.toString();
  }

  return value;
}
 
Example 16
Source File: Text.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * For the given string, returns the number of UTF-8 bytes
 * required to encode the string.
 * @param string text to encode
 * @return number of UTF-8 bytes required to encode
 */
public static int utf8Length(String string) {
  CharacterIterator iter = new StringCharacterIterator(string);
  char ch = iter.first();
  int size = 0;
  while (ch != CharacterIterator.DONE) {
    if ((ch >= 0xD800) && (ch < 0xDC00)) {
      // surrogate pair?
      char trail = iter.next();
      if ((trail > 0xDBFF) && (trail < 0xE000)) {
        // valid pair
        size += 4;
      } else {
        // invalid pair
        size += 3;
        iter.previous(); // rewind one
      }
    } else if (ch < 0x80) {
      size++;
    } else if (ch < 0x800) {
      size += 2;
    } else {
      // ch < 0x10000, that is, the largest char value
      size += 3;
    }
    ch = iter.next();
  }
  return size;
}
 
Example 17
Source File: Text.java    From hadoop-gpu with Apache License 2.0 5 votes vote down vote up
/**
 * For the given string, returns the number of UTF-8 bytes
 * required to encode the string.
 * @param string text to encode
 * @return number of UTF-8 bytes required to encode
 */
public static int utf8Length(String string) {
  CharacterIterator iter = new StringCharacterIterator(string);
  char ch = iter.first();
  int size = 0;
  while (ch != CharacterIterator.DONE) {
    if ((ch >= 0xD800) && (ch < 0xDC00)) {
      // surrogate pair?
      char trail = iter.next();
      if ((trail > 0xDBFF) && (trail < 0xE000)) {
        // valid pair
        size += 4;
      } else {
        // invalid pair
        size += 3;
        iter.previous(); // rewind one
      }
    } else if (ch < 0x80) {
      size++;
    } else if (ch < 0x800) {
      size += 2;
    } else {
      // ch < 0x10000, that is, the largest char value
      size += 3;
    }
    ch = iter.next();
  }
  return size;
}
 
Example 18
Source File: CharacterIteration.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
/**
  * Move the iterator forward to the next code point, and return that code point,
  *   leaving the iterator positioned at char returned.
  *   For Supplementary chars, the iterator is left positioned at the lead surrogate.
  * @param ci  The character iterator
  * @return    The next code point.
  */
 public static int next32(CharacterIterator ci) {
     // If the current position is at a surrogate pair, move to the trail surrogate
     //   which leaves it in position for underlying iterator's next() to work.
     int c = ci.current();
     if (c >= UTF16.LEAD_SURROGATE_MIN_VALUE && c<=UTF16.LEAD_SURROGATE_MAX_VALUE) {
         c = ci.next();   
         if (c<UTF16.TRAIL_SURROGATE_MIN_VALUE || c>UTF16.TRAIL_SURROGATE_MAX_VALUE) {
             ci.previous();   
         }
     }

     // For BMP chars, this next() is the real deal.
     c = ci.next();
     
     // If we might have a lead surrogate, we need to peak ahead to get the trail 
     //  even though we don't want to really be positioned there.
     if (c >= UTF16.LEAD_SURROGATE_MIN_VALUE) {
         c = nextTrail32(ci, c);   
     }
     
     if (c >= UTF16.SUPPLEMENTARY_MIN_VALUE && c != DONE32) {
         // We got a supplementary char.  Back the iterator up to the postion
         // of the lead surrogate.
         ci.previous();   
     }
     return c;
}
 
Example 19
Source File: StringSearch.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
private static int codePointBefore(CharacterIterator iter, int index) {
    int currentIterIndex = iter.getIndex();
    iter.setIndex(index);
    char codeUnit = iter.previous();
    int cp = codeUnit;
    if (Character.isLowSurrogate(codeUnit)) {
        char prevUnit = iter.previous();
        if (Character.isHighSurrogate(prevUnit)) {
            cp = Character.toCodePoint(prevUnit, codeUnit);
        }
    }
    iter.setIndex(currentIterIndex);  // restore iter position
    return cp;
}
 
Example 20
Source File: KHtmlEdit.java    From stendhal with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Extract link content from a character iterator. It is assumed that the
 * '#' has already been eaten. It leaves the character iterator at the first
 * character after the link text.
 *
 * @param ci
 *            The character iterator.
 *
 * @return Link text (or an empty string).
 */
protected String extractLink(final CharacterIterator ci) {
	final StringBuilder sbuf = new StringBuilder();
	char ch = ci.current();
	char terminator = ' ';

	// color quoted compound words like "#'iron sword'"
	if (ch == '\'') {
		terminator = ch;
	}

	while (ch != CharacterIterator.DONE) {
		if (ch == terminator) {
			if (terminator == ' ') {
   				/*
   				 * Continued link (#abc #def)?
   				 */
   				ch = ci.next();

   				if (ch == '#') {
   					ch = ' ';
   				} else {
   					ci.previous();
   					break;
   				}
			} else {
				break;
			}
		}

		sbuf.append(ch);
		ch = ci.next();
	}

	/*
	 * Don't treat word delimiter(s) on the end as link text
	 */
	int len = sbuf.length();

	while (len != 0) {
		if (!isWordDelim(sbuf.charAt(--len))) {
			len++;
			break;
		}

		sbuf.setLength(len);
		ci.previous();
	}

	/*
	 * Nothing found?
	 */
	if (len == 0) {
		return null;
	}

	return sbuf.toString();
}