"Fossies" - the Fresh Open Source Software Archive

Member "OpenSP-1.5.2/lib/UnivCharsetDesc.cxx" (21 Jul 2005, 4262 Bytes) of package /linux/misc/old/OpenSP-1.5.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "UnivCharsetDesc.cxx" see the Fossies "Dox" file reference documentation.

    1 // Copyright (c) 1994, 1997 James Clark
    2 // See the file COPYING for copying permission.
    3 
    4 #ifdef __GNUG__
    5 #pragma implementation
    6 #endif
    7 #include "splib.h"
    8 #include "UnivCharsetDesc.h"
    9 #include "macros.h"
   10 #include "constant.h"
   11 
   12 #ifdef SP_NAMESPACE
   13 namespace SP_NAMESPACE {
   14 #endif
   15 
   16 UnivCharsetDesc::UnivCharsetDesc()
   17 : charMap_(unsigned(1) << 31)
   18 {
   19 }
   20 
   21 UnivCharsetDesc::UnivCharsetDesc(const Range *p, size_t n)
   22 : charMap_(unsigned(1) << 31)
   23 {
   24   set(p, n);
   25 }
   26 
   27 void UnivCharsetDesc::set(const Range *p, size_t n)
   28 {
   29   for (size_t i = 0; i < n; i++) {
   30     const Range &r = p[i];
   31     Char max;
   32     if (r.count > charMax || r.descMin > charMax - r.count)
   33       max = charMax;
   34     else
   35       max = r.descMin + (r.count - 1);
   36     if (max - r.descMin > univCharMax
   37     || r.univMin > univCharMax - (max - r.descMin))
   38       max = r.descMin + (univCharMax - r.univMin);
   39     addRange(r.descMin, max, r.univMin);
   40   }
   41 }
   42 
   43 void UnivCharsetDesc::addRange(WideChar descMin,
   44                    WideChar descMax,
   45                    UnivChar univMin)
   46 {
   47   if (descMin <= charMax) {
   48     Char max = descMax > charMax ? charMax : descMax;
   49     charMap_.setRange(descMin, max, wrapChar(univMin, descMin));
   50   }
   51   if (descMax > charMax) {
   52     if (descMin > charMax)
   53       rangeMap_.addRange(descMin, descMax, univMin);
   54     else
   55       rangeMap_.addRange(charMax, descMax, univMin + (charMax - descMin));
   56   }
   57 }
   58 
   59 void UnivCharsetDesc::addBaseRange(const UnivCharsetDesc &baseSet,
   60                    WideChar descMin,
   61                    WideChar descMax,
   62                    WideChar baseMin,
   63                    ISet<WideChar> &baseMissing)
   64 {
   65   UnivCharsetDescIter iter(baseSet);
   66   iter.skipTo(baseMin);
   67   WideChar baseMax = baseMin + (descMax - descMin);
   68   WideChar iDescMin, iDescMax;
   69   UnivChar iBaseMin;
   70   WideChar missingBaseMin = baseMin;
   71   Boolean usedAll = 0;
   72   while (iter.next(iDescMin, iDescMax, iBaseMin) && iDescMin <= baseMax) {
   73     //  baseMin   baseMax
   74     //          iDescMin iDescMax
   75     if (iDescMax >= baseMin) {
   76       WideChar min = baseMin > iDescMin ? baseMin : iDescMin;
   77       if (min > missingBaseMin)
   78     baseMissing.addRange(missingBaseMin, min - 1);
   79       WideChar max = baseMax < iDescMax ? baseMax : iDescMax;
   80       missingBaseMin = max + 1;
   81       if (missingBaseMin == 0)
   82     usedAll = 1;
   83       ASSERT(min <= max);
   84       addRange(descMin + (min - baseMin),
   85            descMin + (max - baseMin),
   86            iBaseMin + (min - iDescMin));
   87     }
   88   }
   89   if (!usedAll && baseMax >= missingBaseMin)
   90     baseMissing.addRange(missingBaseMin, baseMax);
   91 }
   92 
   93 unsigned UnivCharsetDesc::univToDesc(UnivChar to, WideChar &from,
   94                      ISet<WideChar> &fromSet,
   95                      WideChar &count) const
   96 {
   97   unsigned ret = rangeMap_.inverseMap(to, from, fromSet, count);
   98   Char min = 0;
   99   do {
  100     Char max;
  101     Unsigned32 tem = charMap_.getRange(min, max);
  102     if (!noDesc(tem)) {
  103       UnivChar toMin = extractChar(tem, min);
  104       if (toMin <= to && to <= toMin + (max - min)) {
  105     Char n = min + (to - toMin);
  106     WideChar thisCount = max - n + 1;
  107     if (ret > 1) {
  108       fromSet.add(n);
  109       if (thisCount < count)
  110         count = thisCount;
  111       if (n < from)
  112         from = n;
  113     }
  114     else if (ret == 1) {
  115       fromSet.add(from);
  116       fromSet.add(n);
  117       ret = 2;
  118       if (thisCount < count)
  119         count = thisCount;
  120       if (n < from)
  121         from = n;
  122     }
  123     else {
  124       count = thisCount;
  125       from = n;
  126       ret = 1;
  127     }
  128       }
  129       else if (ret == 0 && toMin > to && toMin - to < count)
  130     count = toMin - to;
  131     }
  132     min = max;
  133   } while (min++ != charMax);
  134   return ret;
  135 }
  136 
  137 UnivCharsetDescIter::UnivCharsetDescIter(const UnivCharsetDesc &desc)
  138 : charMap_(&desc.charMap_), doneCharMap_(0), nextChar_(0),
  139   rangeMapIter_(desc.rangeMap_)
  140 {
  141 }
  142 
  143 Boolean UnivCharsetDescIter::next(WideChar &descMin,
  144                   WideChar &descMax,
  145                   UnivChar &univMin)
  146 {
  147   while (!doneCharMap_) {
  148     Char ch = nextChar_;
  149     Unsigned32 tem = charMap_->getRange(nextChar_, nextChar_);
  150     descMax = nextChar_;
  151     if (!UnivCharsetDesc::noDesc(tem)) {
  152       descMin = ch;
  153       descMax = nextChar_;
  154       univMin = UnivCharsetDesc::extractChar(tem, ch);
  155       if (nextChar_ == charMax)
  156         doneCharMap_ = 1;
  157       else
  158     nextChar_++;
  159       return 1;
  160     }
  161     if (nextChar_ == charMax)
  162       doneCharMap_ = 1;
  163     else
  164       nextChar_++;
  165   }
  166   return rangeMapIter_.next(descMin, descMax, univMin);
  167 }
  168 
  169 #ifdef SP_NAMESPACE
  170 }
  171 #endif