2
*******************************************************************************
3
* Copyright (C) 2001, International Business Machines
4
* Corporation and others. All Rights Reserved.
5
*******************************************************************************
8
* tab size: 8 (not used)
11
* Author: Markus W. Scherer
13
* Modification history:
14
* 05/18/2001 weiv Made into separate module
21
* encode one difference value -0x10ffff..+0x10ffff in 1..3 bytes,
22
* preserving lexical order
25
u_writeDiff(int32_t diff, uint8_t *p) {
26
if(diff>=SLOPE_REACH_NEG_1) {
27
if(diff<=SLOPE_REACH_POS_1) {
28
*p++=(uint8_t)(SLOPE_MIDDLE+diff);
29
} else if(diff<=SLOPE_REACH_POS_2) {
30
*p++=(uint8_t)(SLOPE_START_POS_2+(diff/SLOPE_TAIL_COUNT));
31
*p++=(uint8_t)(SLOPE_MIN+diff%SLOPE_TAIL_COUNT);
32
} else if(diff<=SLOPE_REACH_POS_3) {
33
p[2]=(uint8_t)(SLOPE_MIN+diff%SLOPE_TAIL_COUNT);
34
diff/=SLOPE_TAIL_COUNT;
35
p[1]=(uint8_t)(SLOPE_MIN+diff%SLOPE_TAIL_COUNT);
36
*p=(uint8_t)(SLOPE_START_POS_3+(diff/SLOPE_TAIL_COUNT));
39
p[3]=(uint8_t)(SLOPE_MIN+diff%SLOPE_TAIL_COUNT);
40
diff/=SLOPE_TAIL_COUNT;
41
p[2]=(uint8_t)(SLOPE_MIN+diff%SLOPE_TAIL_COUNT);
42
diff/=SLOPE_TAIL_COUNT;
43
p[1]=(uint8_t)(SLOPE_MIN+diff%SLOPE_TAIL_COUNT);
50
if(diff>=SLOPE_REACH_NEG_2) {
51
NEGDIVMOD(diff, SLOPE_TAIL_COUNT, m);
52
*p++=(uint8_t)(SLOPE_START_NEG_2+diff);
53
*p++=(uint8_t)(SLOPE_MIN+m);
54
} else if(diff>=SLOPE_REACH_NEG_3) {
55
NEGDIVMOD(diff, SLOPE_TAIL_COUNT, m);
56
p[2]=(uint8_t)(SLOPE_MIN+m);
57
NEGDIVMOD(diff, SLOPE_TAIL_COUNT, m);
58
p[1]=(uint8_t)(SLOPE_MIN+m);
59
*p=(uint8_t)(SLOPE_START_NEG_3+diff);
62
NEGDIVMOD(diff, SLOPE_TAIL_COUNT, m);
63
p[3]=(uint8_t)(SLOPE_MIN+m);
64
NEGDIVMOD(diff, SLOPE_TAIL_COUNT, m);
65
p[2]=(uint8_t)(SLOPE_MIN+m);
66
NEGDIVMOD(diff, SLOPE_TAIL_COUNT, m);
67
p[1]=(uint8_t)(SLOPE_MIN+m);
75
/* How many bytes would writeDiff() write? */
77
lengthOfDiff(int32_t diff) {
78
if(diff>=SLOPE_REACH_NEG_1) {
79
if(diff<=SLOPE_REACH_POS_1) {
81
} else if(diff<=SLOPE_REACH_POS_2) {
83
} else if(diff<=SLOPE_REACH_POS_3) {
89
if(diff>=SLOPE_REACH_NEG_2) {
91
} else if(diff>=SLOPE_REACH_NEG_3) {
100
* Encode the code points of a string as
101
* a sequence of byte-encoded differences (slope detection),
102
* preserving lexical order.
104
* Optimize the difference-taking for runs of Unicode text within
107
* Most small scripts are allocated within aligned 128-blocks of Unicode
108
* code points. Lexical order is preserved if "prev" is always moved
109
* into the middle of such a block.
111
* Additionally, "prev" is moved from anywhere in the Unihan
112
* area into the middle of that area.
113
* Note that the identical-level run in a sort key is generated from
114
* NFD text - there are never Hangul characters included.
117
u_writeIdenticalLevelRun(const UChar *s, int32_t length, uint8_t *p) {
126
if(prev<0x4e00 || prev>=0xa000) {
127
prev=(prev&~0x7f)-SLOPE_REACH_NEG_1;
130
* Unihan U+4e00..U+9fa5:
131
* double-bytes down from the upper end
133
prev=0x9fff-SLOPE_REACH_POS_2;
136
UTF_NEXT_CHAR(s, i, length, c);
137
p=u_writeDiff(c-prev, p);
143
/* How many bytes would writeIdenticalLevelRun() write? */
145
u_lengthOfIdenticalLevelRun(const UChar *s, int32_t length) {
147
int32_t i, runLength;
153
if(prev<0x4e00 || prev>=0xa000) {
154
prev=(prev&~0x7f)-SLOPE_REACH_NEG_1;
157
* Unihan U+4e00..U+9fa5:
158
* double-bytes down from the upper end
160
prev=0x9fff-SLOPE_REACH_POS_2;
163
UTF_NEXT_CHAR(s, i, length, c);
164
runLength+=lengthOfDiff(c-prev);