Logo Search packages:      
Sourcecode: icu version File versions

int32_t UnicodeString::moveIndex32 ( int32_t  index,
int32_t  delta 
) const

Move the code unit index along the string by delta code points. Interpret the input index as a code unit-based offset into the string, move the index forward or backward by delta code points, and return the resulting index. The input index should point to the first code unit of a code point, if there is more than one.

Both input and output indexes are code unit-based as for all string indexes/offsets in ICU (and other libraries, like MBCS char*). If delta<0 then the index is moved backward (toward the start of the string). If delta>0 then the index is moved forward (toward the end of the string).

This behaves like CharacterIterator::move32(delta, kCurrent).

Behavior for out-of-bounds indexes: moveIndex32 pins the input index to 0..length(), i.e., if the input index<0 then it is pinned to 0; if it is index>length() then it is pinned to length(). Afterwards, the index is moved by delta code points forward or backward, but no further backward than to 0 and no further forward than to length(). The resulting index return value will be in between 0 and length(), inclusively.


 // s has code points 'a' U+10000 'b' U+10ffff U+2029
 UnicodeString s=UNICODE_STRING("a\\U00010000b\\U0010ffff\\u2029", 31).unescape();

 // initial index: position of U+10000
 int32_t index=1;

 // the following examples will all result in index==4, position of U+10ffff

 // skip 2 code points from some position in the string
 index=s.moveIndex32(index, 2); // skips U+10000 and 'b'

 // go to the 3rd code point from the start of s (0-based)
 index=s.moveIndex32(0, 3); // skips 'a', U+10000, and 'b'

 // go to the next-to-last code point of s
 index=s.moveIndex32(s.length(), -2); // backward-skips U+2029 and U+10ffff

index input code unit index
delta (signed) code point count to move the index forward or backward in the string
the resulting code unit index ICU 2.0

Definition at line 706 of file unistr.cpp.

References length(), UTF_BACK_N, and UTF_FWD_N.

Referenced by RegexMatcher::appendReplacement(), RegexMatcher::find(), and UnicodeStringTest::TestBasicManipulation().

  // pin index
  int32_t len = length();
  if(index<0) {
  } else if(index>len) {

  const UChar *array = getArrayStart();
  if(delta>0) {
    UTF_FWD_N(array, index, len, delta);
  } else {
    UTF_BACK_N(array, 0, index, -delta);

  return index;

Generated by  Doxygen 1.6.0   Back to index