virtual int32_t move32(int32_t delta, CharacterIterator::EOrigin origin){ switch(origin) { case kStart: pos = begin; if(delta > 0) { U16_FWD_N(text, pos, end, delta); } break; case kCurrent: if(delta > 0) { U16_FWD_N(text, pos, end, delta); } else { U16_BACK_N(text, begin, pos, -delta); } break; case kEnd: pos = end; if(delta < 0) { U16_BACK_N(text, begin, pos, -delta); } break; default: break; } return pos; };
int utf16_cp_to_cu(const UChar *ustring, int32_t ustring_len, long cp_offset, int32_t *cu_offset, UErrorCode *status) { if (0 != cp_offset) { int32_t _cp_count = u_countChar32(ustring, ustring_len); if (cp_offset < 0) { if (cp_offset < -_cp_count) { *status = U_INDEX_OUTOFBOUNDS_ERROR; return FAILURE; } *cu_offset = ustring_len; U16_BACK_N(ustring, 0, *cu_offset, -cp_offset); } else { if (cp_offset >= _cp_count) { *status = U_INDEX_OUTOFBOUNDS_ERROR; return FAILURE; } U16_FWD_N(ustring, *cu_offset, ustring_len, cp_offset); } } return SUCCESS; }
static void TestFwdBack(){ static UChar input[]={0x0061, 0xd800, 0xdc00, 0xdbff, 0xdfff, 0x0062, 0xd841, 0xd7ff, 0xd841, 0xdc41, 0xdc00, 0x0000}; static uint16_t fwd_unsafe[] ={1, 3, 5, 6, 8, 10, 11, 12}; static uint16_t fwd_safe[] ={1, 3, 5, 6, 7, 8, 10, 11, 12}; static uint16_t back_unsafe[]={11, 9, 8, 7, 6, 5, 3, 1, 0}; static uint16_t back_safe[] ={11, 10, 8, 7, 6, 5, 3, 1, 0}; static uint16_t Nvalue[]= {0, 1, 2, 3, 1, 2, 1}; static uint16_t fwd_N_unsafe[] ={0, 1, 5, 10, 11}; static uint16_t fwd_N_safe[] ={0, 1, 5, 8, 10, 12, 12}; /*safe macro keeps it at the end of the string */ static uint16_t back_N_unsafe[]={12, 11, 8, 5, 3}; static uint16_t back_N_safe[] ={12, 11, 8, 5, 3, 0, 0}; uint16_t offunsafe=0, offsafe=0; uint16_t i=0; while(offunsafe < sizeof(input)/U_SIZEOF_UCHAR){ UTF16_FWD_1_UNSAFE(input, offunsafe); if(offunsafe != fwd_unsafe[i]){ log_err("ERROR: Forward_unsafe offset expected:%d, Got:%d\n", fwd_unsafe[i], offunsafe); } i++; } offunsafe=0, offsafe=0; i=0; while(offunsafe < sizeof(input)/U_SIZEOF_UCHAR){ U16_FWD_1_UNSAFE(input, offunsafe); if(offunsafe != fwd_unsafe[i]){ log_err("ERROR: U16_FWD_1_UNSAFE offset expected:%d, Got:%d\n", fwd_unsafe[i], offunsafe); } i++; } i=0; while(offsafe < sizeof(input)/U_SIZEOF_UCHAR){ UTF16_FWD_1_SAFE(input, offsafe, sizeof(input)/U_SIZEOF_UCHAR); if(offsafe != fwd_safe[i]){ log_err("ERROR: Forward_safe offset expected:%d, Got:%d\n", fwd_safe[i], offsafe); } i++; } i=0; while(offsafe < sizeof(input)/U_SIZEOF_UCHAR){ U16_FWD_1(input, offsafe, sizeof(input)/U_SIZEOF_UCHAR); if(offsafe != fwd_safe[i]){ log_err("ERROR: U16_FWD_1 offset expected:%d, Got:%d\n", fwd_safe[i], offsafe); } i++; } offunsafe=sizeof(input)/U_SIZEOF_UCHAR; offsafe=sizeof(input)/U_SIZEOF_UCHAR; i=0; while(offunsafe > 0){ UTF16_BACK_1_UNSAFE(input, offunsafe); if(offunsafe != back_unsafe[i]){ log_err("ERROR: Backward_unsafe offset expected:%d, Got:%d\n", back_unsafe[i], offunsafe); } i++; } offunsafe=sizeof(input)/U_SIZEOF_UCHAR; offsafe=sizeof(input)/U_SIZEOF_UCHAR; i=0; while(offunsafe > 0){ U16_BACK_1_UNSAFE(input, offunsafe); if(offunsafe != back_unsafe[i]){ log_err("ERROR: U16_BACK_1_UNSAFE offset expected:%d, Got:%d\n", back_unsafe[i], offunsafe); } i++; } i=0; while(offsafe > 0){ UTF16_BACK_1_SAFE(input,0, offsafe); if(offsafe != back_safe[i]){ log_err("ERROR: Backward_safe offset expected:%d, Got:%d\n", back_unsafe[i], offsafe); } i++; } i=0; while(offsafe > 0){ U16_BACK_1(input,0, offsafe); if(offsafe != back_safe[i]){ log_err("ERROR: U16_BACK_1 offset expected:%d, Got:%d\n", back_unsafe[i], offsafe); } i++; } offunsafe=0; offsafe=0; for(i=0; i<sizeof(Nvalue)/sizeof(Nvalue[0])-2; i++){ /*didn't want it to fail(we assume 0<i<length)*/ UTF16_FWD_N_UNSAFE(input, offunsafe, Nvalue[i]); if(offunsafe != fwd_N_unsafe[i]){ log_err("ERROR: Forward_N_unsafe offset expected:%d, Got:%d\n", fwd_N_unsafe[i], offunsafe); } } offunsafe=0; for(i=0; i<sizeof(Nvalue)/sizeof(Nvalue[0])-2; i++){ /*didn't want it to fail(we assume 0<i<length)*/ U16_FWD_N_UNSAFE(input, offunsafe, Nvalue[i]); if(offunsafe != fwd_N_unsafe[i]){ log_err("ERROR: U16_FWD_N_UNSAFE offset expected:%d, Got:%d\n", fwd_N_unsafe[i], offunsafe); } } offsafe=0; for(i=0; i<sizeof(Nvalue)/sizeof(Nvalue[0]); i++){ UTF16_FWD_N_SAFE(input, offsafe, sizeof(input)/U_SIZEOF_UCHAR, Nvalue[i]); if(offsafe != fwd_N_safe[i]){ log_err("ERROR: Forward_N_safe offset expected:%d, Got:%d\n", fwd_N_safe[i], offsafe); } } offsafe=0; for(i=0; i<sizeof(Nvalue)/sizeof(Nvalue[0]); i++){ U16_FWD_N(input, offsafe, sizeof(input)/U_SIZEOF_UCHAR, Nvalue[i]); if(offsafe != fwd_N_safe[i]){ log_err("ERROR: U16_FWD_N offset expected:%d, Got:%d\n", fwd_N_safe[i], offsafe); } } offunsafe=sizeof(input)/U_SIZEOF_UCHAR; for(i=0; i<sizeof(Nvalue)/sizeof(Nvalue[0])-2; i++){ UTF16_BACK_N_UNSAFE(input, offunsafe, Nvalue[i]); if(offunsafe != back_N_unsafe[i]){ log_err("ERROR: backward_N_unsafe offset expected:%d, Got:%d\n", back_N_unsafe[i], offunsafe); } } offunsafe=sizeof(input)/U_SIZEOF_UCHAR; for(i=0; i<sizeof(Nvalue)/sizeof(Nvalue[0])-2; i++){ U16_BACK_N_UNSAFE(input, offunsafe, Nvalue[i]); if(offunsafe != back_N_unsafe[i]){ log_err("ERROR: U16_BACK_N_UNSAFE offset expected:%d, Got:%d\n", back_N_unsafe[i], offunsafe); } } offsafe=sizeof(input)/U_SIZEOF_UCHAR; for(i=0; i<sizeof(Nvalue)/sizeof(Nvalue[0]); i++){ UTF16_BACK_N_SAFE(input, 0, offsafe, Nvalue[i]); if(offsafe != back_N_safe[i]){ log_err("ERROR: backward_N_safe offset expected:%d, Got:%d\n", back_N_safe[i], offsafe); } } offsafe=sizeof(input)/U_SIZEOF_UCHAR; for(i=0; i<sizeof(Nvalue)/sizeof(Nvalue[0]); i++){ U16_BACK_N(input, 0, offsafe, Nvalue[i]); if(offsafe != back_N_safe[i]){ log_err("ERROR: U16_BACK_N offset expected:%d, Got:%d\n", back_N_safe[i], offsafe); } } }