Home | History | Annotate | Download | only in i18n
      1 /*
      2 *******************************************************************************
      3 *   Copyright (C) 2001-2011, 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 #include "unicode/bytestream.h"
     20 
     21 #if !UCONFIG_NO_COLLATION
     22 
     23 #include "bocsu.h"
     24 
     25 /*
     26  * encode one difference value -0x10ffff..+0x10ffff in 1..3 bytes,
     27  * preserving lexical order
     28  */
     29 U_CFUNC uint8_t *
     30 u_writeDiff(int32_t diff, uint8_t *p) {
     31     if(diff>=SLOPE_REACH_NEG_1) {
     32         if(diff<=SLOPE_REACH_POS_1) {
     33             *p++=(uint8_t)(SLOPE_MIDDLE+diff);
     34         } else if(diff<=SLOPE_REACH_POS_2) {
     35             *p++=(uint8_t)(SLOPE_START_POS_2+(diff/SLOPE_TAIL_COUNT));
     36             *p++=(uint8_t)(SLOPE_MIN+diff%SLOPE_TAIL_COUNT);
     37         } else if(diff<=SLOPE_REACH_POS_3) {
     38             p[2]=(uint8_t)(SLOPE_MIN+diff%SLOPE_TAIL_COUNT);
     39             diff/=SLOPE_TAIL_COUNT;
     40             p[1]=(uint8_t)(SLOPE_MIN+diff%SLOPE_TAIL_COUNT);
     41             *p=(uint8_t)(SLOPE_START_POS_3+(diff/SLOPE_TAIL_COUNT));
     42             p+=3;
     43         } else {
     44             p[3]=(uint8_t)(SLOPE_MIN+diff%SLOPE_TAIL_COUNT);
     45             diff/=SLOPE_TAIL_COUNT;
     46             p[2]=(uint8_t)(SLOPE_MIN+diff%SLOPE_TAIL_COUNT);
     47             diff/=SLOPE_TAIL_COUNT;
     48             p[1]=(uint8_t)(SLOPE_MIN+diff%SLOPE_TAIL_COUNT);
     49             *p=SLOPE_MAX;
     50             p+=4;
     51         }
     52     } else {
     53         int32_t m;
     54 
     55         if(diff>=SLOPE_REACH_NEG_2) {
     56             NEGDIVMOD(diff, SLOPE_TAIL_COUNT, m);
     57             *p++=(uint8_t)(SLOPE_START_NEG_2+diff);
     58             *p++=(uint8_t)(SLOPE_MIN+m);
     59         } else if(diff>=SLOPE_REACH_NEG_3) {
     60             NEGDIVMOD(diff, SLOPE_TAIL_COUNT, m);
     61             p[2]=(uint8_t)(SLOPE_MIN+m);
     62             NEGDIVMOD(diff, SLOPE_TAIL_COUNT, m);
     63             p[1]=(uint8_t)(SLOPE_MIN+m);
     64             *p=(uint8_t)(SLOPE_START_NEG_3+diff);
     65             p+=3;
     66         } else {
     67             NEGDIVMOD(diff, SLOPE_TAIL_COUNT, m);
     68             p[3]=(uint8_t)(SLOPE_MIN+m);
     69             NEGDIVMOD(diff, SLOPE_TAIL_COUNT, m);
     70             p[2]=(uint8_t)(SLOPE_MIN+m);
     71             NEGDIVMOD(diff, SLOPE_TAIL_COUNT, m);
     72             p[1]=(uint8_t)(SLOPE_MIN+m);
     73             *p=SLOPE_MIN;
     74             p+=4;
     75         }
     76     }
     77     return p;
     78 }
     79 
     80 /*
     81  * Encode the code points of a string as
     82  * a sequence of byte-encoded differences (slope detection),
     83  * preserving lexical order.
     84  *
     85  * Optimize the difference-taking for runs of Unicode text within
     86  * small scripts:
     87  *
     88  * Most small scripts are allocated within aligned 128-blocks of Unicode
     89  * code points. Lexical order is preserved if "prev" is always moved
     90  * into the middle of such a block.
     91  *
     92  * Additionally, "prev" is moved from anywhere in the Unihan
     93  * area into the middle of that area.
     94  * Note that the identical-level run in a sort key is generated from
     95  * NFD text - there are never Hangul characters included.
     96  */
     97 U_CFUNC void
     98 u_writeIdenticalLevelRun(const UChar *s, int32_t length, U_NAMESPACE_QUALIFIER ByteSink &sink) {
     99     char scratch[64];
    100     int32_t capacity;
    101 
    102     UChar32 prev=0;
    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             p=u_writeDiff(c-prev, p);
    130             prev=c;
    131         }
    132         sink.Append(buffer, (int32_t)(p-reinterpret_cast<uint8_t *>(buffer)));
    133     }
    134 }
    135 
    136 U_CFUNC int32_t
    137 u_writeIdenticalLevelRunTwoChars(UChar32 first, UChar32 second, uint8_t *p) {
    138     uint8_t *p0 = p;
    139     if(first<0x4e00 || first>=0xa000) {
    140         first=(first&~0x7f)-SLOPE_REACH_NEG_1;
    141     } else {
    142         /*
    143          * Unihan U+4e00..U+9fa5:
    144          * double-bytes down from the upper end
    145          */
    146         first=0x9fff-SLOPE_REACH_POS_2;
    147     }
    148 
    149     p=u_writeDiff(second-first, p);
    150     return (int32_t)(p-p0);
    151 }
    152 
    153 #endif /* #if !UCONFIG_NO_COLLATION */
    154