UGooString.cc 3.81 KB
Newer Older
Albert Astals Cid's avatar
 
Albert Astals Cid committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//========================================================================
//
// UGooString.cc
//
// Unicode string
//
// Copyright 2005 Albert Astals Cid <aacid@kde.org>
//
//========================================================================

#include <string.h>

#include "goo/gmem.h"
#include "goo/GooString.h"
#include "PDFDocEncoding.h"
#include "UGooString.h"

Albert Astals Cid's avatar
Albert Astals Cid committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
int inline UGooString::roundedSize(int len) {
  int delta;
  if (len <= STR_STATIC_SIZE-1)
      return STR_STATIC_SIZE;
  delta = len < 256 ? 7 : 255;
  return ((len + 1) + delta) & ~delta;
}

// Make sure that the buffer is big enough to contain <newLength> characters
// plus terminating 0.
// We assume that if this is being called from the constructor, <s> was set
// to NULL and <length> was set to 0 to indicate unused string before calling us.
void inline UGooString::resize(int newLength) {
  Unicode *s1 = s;

  if (!s || (roundedSize(length) != roundedSize(newLength))) {
    // requires re-allocating data for string
    if (newLength < STR_STATIC_SIZE)
        s1 = sStatic;
    else
        s1 = new Unicode[roundedSize(newLength)];

    // we had to re-allocate the memory, so copy the content of previous
    // buffer into a new buffer
    if (s) {
      if (newLength < length) {
        memcpy(s1, s, newLength);
      } else {
        memcpy(s1, s, length);
      }
    }
    if (s != sStatic)
      delete[] s;
  }

  s = s1;
  length = newLength;
  s[length] = '\0';
}

UGooString::UGooString()
Albert Astals Cid's avatar
 
Albert Astals Cid committed
59
{
Albert Astals Cid's avatar
Albert Astals Cid committed
60 61 62
  s = NULL;
  length = 0;
  resize(0);
Albert Astals Cid's avatar
 
Albert Astals Cid committed
63 64 65 66
}

UGooString::UGooString(GooString &str)
{
Albert Astals Cid's avatar
Albert Astals Cid committed
67 68 69
  s = NULL;
  length = 0;
  if (str.hasUnicodeMarker())
Albert Astals Cid's avatar
 
Albert Astals Cid committed
70
  {
Albert Astals Cid's avatar
Albert Astals Cid committed
71
    resize((str.getLength() - 2) / 2);
Albert Astals Cid's avatar
 
Albert Astals Cid committed
72 73 74 75
    for (int j = 0; j < length; ++j) {
      s[j] = ((str.getChar(2 + 2*j) & 0xff) << 8) | (str.getChar(3 + 2*j) & 0xff);
    }
  } else
Albert Astals Cid's avatar
Albert Astals Cid committed
76 77 78 79 80 81 82
    Set(str.getCString(), str.getLength());
}

UGooString::UGooString(Unicode *u, int strLen)
{
  resize(strLen);
  s = u;
Albert Astals Cid's avatar
 
Albert Astals Cid committed
83 84 85 86
}

UGooString::UGooString(const UGooString &str)
{
Albert Astals Cid's avatar
Albert Astals Cid committed
87 88 89
  s = NULL;
  length = 0;
  Set(str);
Albert Astals Cid's avatar
 
Albert Astals Cid committed
90 91
}

Albert Astals Cid's avatar
Albert Astals Cid committed
92
UGooString::UGooString(const char *str, int strLen)
Albert Astals Cid's avatar
 
Albert Astals Cid committed
93
{
Albert Astals Cid's avatar
Albert Astals Cid committed
94 95 96 97 98
  s = NULL;
  length = 0;
  if (CALC_STRING_LEN == strLen)
    strLen = strlen(str);
  Set(str, strLen);
Albert Astals Cid's avatar
 
Albert Astals Cid committed
99 100
}

Albert Astals Cid's avatar
Albert Astals Cid committed
101
UGooString *UGooString::Set(const UGooString &str)
Albert Astals Cid's avatar
 
Albert Astals Cid committed
102
{
Albert Astals Cid's avatar
Albert Astals Cid committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
  resize(str.length);
  memcpy(s, str.s, length * sizeof(Unicode));
  return this;
}

UGooString* UGooString::Set(const char *str, int strLen)
{
  int  j;
  bool foundUnencoded = false;

  if (CALC_STRING_LEN == strLen)
    strLen = strlen(str);

  resize(strLen);
  for (j = 0; !foundUnencoded && j < length; ++j) {
    s[j] = pdfDocEncoding[str[j] & 0xff];
    if (!s[j]) {
        foundUnencoded = true;
        break;
    }
123
  }
Albert Astals Cid's avatar
Albert Astals Cid committed
124
  if ( foundUnencoded )
125
  {
Albert Astals Cid's avatar
Albert Astals Cid committed
126 127
    for (j = 0; j < length; ++j) {
      s[j] = str[j];
128
    }
Albert Astals Cid's avatar
 
Albert Astals Cid committed
129
  }
Albert Astals Cid's avatar
Albert Astals Cid committed
130 131 132 133 134 135 136
  return this;
}

UGooString *UGooString::clear()
{
    resize(0);
    return this;
Albert Astals Cid's avatar
 
Albert Astals Cid committed
137 138 139 140
}

UGooString::~UGooString()
{
Albert Astals Cid's avatar
Albert Astals Cid committed
141 142
  if (s != sStatic)
    delete[] s;
Albert Astals Cid's avatar
 
Albert Astals Cid committed
143 144
}

Albert Astals Cid's avatar
Albert Astals Cid committed
145 146 147 148 149 150
int UGooString::cmp(const UGooString &str) const
{
    return cmp(&str);
}

int UGooString::cmp(const UGooString *str) const
Albert Astals Cid's avatar
 
Albert Astals Cid committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
{
  int n1, n2, i, x;
  Unicode *p1, *p2;

  n1 = length;
  n2 = str->length;
  for (i = 0, p1 = s, p2 = str->s; i < n1 && i < n2; ++i, ++p1, ++p2) {
    x = *p1 - *p2;
    if (x != 0) {
      return x;
    }
  }
  return n1 - n2;
}

Albert Astals Cid's avatar
Albert Astals Cid committed
166 167 168 169 170 171 172 173
// FIXME: 
// a) this is confusing because GooString::getCSTring() returns a pointer
//    but UGooString returns a newly allocated copy. Should give this
//    a different name, like copyAsAscii() or copyAsGooString()
// b) this interface requires copying. It should be changed to take a
//    GooString& as a param and put the data inside it so that it uses
//    caching optimization of GooString. Callers should be changed to use
//    this new interface
Albert Astals Cid's avatar
 
Albert Astals Cid committed
174 175 176 177 178 179 180
char *UGooString::getCString() const
{
  char *res = new char[length + 1];
  for (int i = 0; i < length; i++) res[i] = s[i];
  res[length] = '\0';
  return res;
}
Albert Astals Cid's avatar
Albert Astals Cid committed
181