1/*
2*******************************************************************************
3*   Copyright (C) 2001-2014, International Business Machines
4*   Corporation and others.  All Rights Reserved.
5*******************************************************************************
6*   file name:  bocsu.cpp
7*   encoding:   US-ASCII
8*   tab size:   8 (not used)
9*   indentation:4
10*
11*   Author: Markus W. Scherer
12*
13*   Modification history:
14*   05/18/2001  weiv    Made into separate module
15*/
16
17
18#include "unicode/utypes.h"
19
20#if !UCONFIG_NO_COLLATION
21
22#include "unicode/bytestream.h"
23#include "unicode/utf16.h"
24#include "bocsu.h"
25
26/*
27 * encode one difference value -0x10ffff..+0x10ffff in 1..4 bytes,
28 * preserving lexical order
29 */
30static uint8_t *
31u_writeDiff(int32_t diff, uint8_t *p) {
32    if(diff>=SLOPE_REACH_NEG_1) {
33        if(diff<=SLOPE_REACH_POS_1) {
34            *p++=(uint8_t)(SLOPE_MIDDLE+diff);
35        } else if(diff<=SLOPE_REACH_POS_2) {
36            *p++=(uint8_t)(SLOPE_START_POS_2+(diff/SLOPE_TAIL_COUNT));
37            *p++=(uint8_t)(SLOPE_MIN+diff%SLOPE_TAIL_COUNT);
38        } else if(diff<=SLOPE_REACH_POS_3) {
39            p[2]=(uint8_t)(SLOPE_MIN+diff%SLOPE_TAIL_COUNT);
40            diff/=SLOPE_TAIL_COUNT;
41            p[1]=(uint8_t)(SLOPE_MIN+diff%SLOPE_TAIL_COUNT);
42            *p=(uint8_t)(SLOPE_START_POS_3+(diff/SLOPE_TAIL_COUNT));
43            p+=3;
44        } else {
45            p[3]=(uint8_t)(SLOPE_MIN+diff%SLOPE_TAIL_COUNT);
46            diff/=SLOPE_TAIL_COUNT;
47            p[2]=(uint8_t)(SLOPE_MIN+diff%SLOPE_TAIL_COUNT);
48            diff/=SLOPE_TAIL_COUNT;
49            p[1]=(uint8_t)(SLOPE_MIN+diff%SLOPE_TAIL_COUNT);
50            *p=SLOPE_MAX;
51            p+=4;
52        }
53    } else {
54        int32_t m;
55
56        if(diff>=SLOPE_REACH_NEG_2) {
57            NEGDIVMOD(diff, SLOPE_TAIL_COUNT, m);
58            *p++=(uint8_t)(SLOPE_START_NEG_2+diff);
59            *p++=(uint8_t)(SLOPE_MIN+m);
60        } else if(diff>=SLOPE_REACH_NEG_3) {
61            NEGDIVMOD(diff, SLOPE_TAIL_COUNT, m);
62            p[2]=(uint8_t)(SLOPE_MIN+m);
63            NEGDIVMOD(diff, SLOPE_TAIL_COUNT, m);
64            p[1]=(uint8_t)(SLOPE_MIN+m);
65            *p=(uint8_t)(SLOPE_START_NEG_3+diff);
66            p+=3;
67        } else {
68            NEGDIVMOD(diff, SLOPE_TAIL_COUNT, m);
69            p[3]=(uint8_t)(SLOPE_MIN+m);
70            NEGDIVMOD(diff, SLOPE_TAIL_COUNT, m);
71            p[2]=(uint8_t)(SLOPE_MIN+m);
72            NEGDIVMOD(diff, SLOPE_TAIL_COUNT, m);
73            p[1]=(uint8_t)(SLOPE_MIN+m);
74            *p=SLOPE_MIN;
75            p+=4;
76        }
77    }
78    return p;
79}
80
81/*
82 * Encode the code points of a string as
83 * a sequence of byte-encoded differences (slope detection),
84 * preserving lexical order.
85 *
86 * Optimize the difference-taking for runs of Unicode text within
87 * small scripts:
88 *
89 * Most small scripts are allocated within aligned 128-blocks of Unicode
90 * code points. Lexical order is preserved if "prev" is always moved
91 * into the middle of such a block.
92 *
93 * Additionally, "prev" is moved from anywhere in the Unihan
94 * area into the middle of that area.
95 * Note that the identical-level run in a sort key is generated from
96 * NFD text - there are never Hangul characters included.
97 */
98U_CFUNC UChar32
99u_writeIdenticalLevelRun(UChar32 prev, const UChar *s, int32_t length, icu::ByteSink &sink) {
100    char scratch[64];
101    int32_t capacity;
102
103    int32_t i=0;
104    while(i<length) {
105        char *buffer=sink.GetAppendBuffer(1, length*2, scratch, (int32_t)sizeof(scratch), &capacity);
106        uint8_t *p;
107        // We must have capacity>=SLOPE_MAX_BYTES in case u_writeDiff() writes that much,
108        // but we do not want to force the sink.GetAppendBuffer() to allocate
109        // for a large min_capacity because we might actually only write one byte.
110        if(capacity<16) {
111            buffer=scratch;
112            capacity=(int32_t)sizeof(scratch);
113        }
114        p=reinterpret_cast<uint8_t *>(buffer);
115        uint8_t *lastSafe=p+capacity-SLOPE_MAX_BYTES;
116        while(i<length && p<=lastSafe) {
117            if(prev<0x4e00 || prev>=0xa000) {
118                prev=(prev&~0x7f)-SLOPE_REACH_NEG_1;
119            } else {
120                /*
121                 * Unihan U+4e00..U+9fa5:
122                 * double-bytes down from the upper end
123                 */
124                prev=0x9fff-SLOPE_REACH_POS_2;
125            }
126
127            UChar32 c;
128            U16_NEXT(s, i, length, c);
129            if(c==0xfffe) {
130                *p++=2;  // merge separator
131                prev=0;
132            } else {
133                p=u_writeDiff(c-prev, p);
134                prev=c;
135            }
136        }
137        sink.Append(buffer, (int32_t)(p-reinterpret_cast<uint8_t *>(buffer)));
138    }
139    return prev;
140}
141
142#endif /* #if !UCONFIG_NO_COLLATION */
143