~ubuntu-branches/ubuntu/breezy/koffice/breezy

« back to all changes in this revision

Viewing changes to filters/kspread/excel/sidewinder/ustring.h

  • Committer: Bazaar Package Importer
  • Author(s): Ben Burton
  • Date: 2004-05-09 11:33:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040509113300-vfrdadqsvjfuhn3b
Tags: 1:1.3.1-1
* New upstream bugfix release.
* Built against newer imagemagick (closes: #246623).
* Made koffice-libs/kformula recommend/depend on latex-xft-fonts, which
  provides mathematical fonts that the formula editor can use.  Also
  patched the kformula part to make these fonts the default.
* Changed kword menu hint from "WordProcessors" to "Word processors"
  (closes: #246209).
* Spellchecker configuration is now fixed (closes: #221256, #227568).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- c-basic-offset: 2 -*-
 
2
/*
 
3
 *  This file is part of the KDE libraries
 
4
 *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
 
5
 *
 
6
 *  This library is free software; you can redistribute it and/or
 
7
 *  modify it under the terms of the GNU Library General Public
 
8
 *  License as published by the Free Software Foundation; either
 
9
 *  version 2 of the License, or (at your option) any later version.
 
10
 *
 
11
 *  This library is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 *  Library General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU Library General Public License
 
17
 *  along with this library; see the file COPYING.LIB.  If not, write to
 
18
 *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
19
 *  Boston, MA 02111-1307, USA.
 
20
 */
 
21
 
 
22
#ifndef SIDEWINDER_USTRING_H_
 
23
#define SIDEWINDER_USTRING_H_
 
24
 
 
25
namespace Sidewinder {
 
26
 
 
27
  /**
 
28
   * @return True if d is not a number (platform support required).
 
29
   */
 
30
  bool isNaN(double d);
 
31
 
 
32
  bool isPosInf(double d);
 
33
  bool isNegInf(double d);
 
34
 
 
35
  class UCharReference;
 
36
  class UString;
 
37
  class UConstString;
 
38
 
 
39
  /**
 
40
   * @short Unicode character.
 
41
   *
 
42
   * UChar represents a 16 bit Unicode character. It's internal data
 
43
   * representation is compatible to XChar2b and QChar. It's therefore
 
44
   * possible to exchange data with X and Qt with shallow copies.
 
45
   */
 
46
  struct UChar {
 
47
    /**
 
48
     * Construct a character with value 0.
 
49
     */
 
50
    UChar();
 
51
    /**
 
52
     * Construct a character with the value denoted by the arguments.
 
53
     * @param h higher byte
 
54
     * @param l lower byte
 
55
     */
 
56
    UChar(unsigned char h , unsigned char l);
 
57
    /**
 
58
     * Construct a character with the given value.
 
59
     * @param u 16 bit Unicode value
 
60
     */
 
61
    UChar(unsigned short u);
 
62
    UChar(const UCharReference &c);
 
63
    /**
 
64
     * @return The higher byte of the character.
 
65
     */
 
66
    unsigned char high() const { return uc >> 8; }
 
67
    /**
 
68
     * @return The lower byte of the character.
 
69
     */
 
70
    unsigned char low() const { return uc & 0xFF; }
 
71
    /**
 
72
     * @return the 16 bit Unicode value of the character
 
73
     */
 
74
    unsigned short unicode() const { return uc; }
 
75
  public:
 
76
    /**
 
77
     * @return The character converted to lower case.
 
78
     */
 
79
    UChar toLower() const;
 
80
    /**
 
81
     * @return The character converted to upper case.
 
82
     */
 
83
    UChar toUpper() const;
 
84
    /**
 
85
     * A static instance of UChar(0).
 
86
     */
 
87
    static UChar null;
 
88
  private:
 
89
    friend class UCharReference;
 
90
    friend class UString;
 
91
    friend bool operator==(const UChar &c1, const UChar &c2);
 
92
    friend bool operator==(const UString& s1, const char *s2);
 
93
    friend bool operator<(const UString& s1, const UString& s2);
 
94
 
 
95
    unsigned short uc;
 
96
  };
 
97
 
 
98
  inline UChar::UChar() : uc(0) { }
 
99
  inline UChar::UChar(unsigned char h , unsigned char l) : uc(h << 8 | l) { }
 
100
  inline UChar::UChar(unsigned short u) : uc(u) { }
 
101
 
 
102
  /**
 
103
   * @short Dynamic reference to a string character.
 
104
   *
 
105
   * UCharReference is the dynamic counterpart of @ref UChar. It's used when
 
106
   * characters retrieved via index from a @ref UString are used in an
 
107
   * assignment expression (and therefore can't be treated as being const):
 
108
   * <pre>
 
109
   * UString s("hello world");
 
110
   * s[0] = 'H';
 
111
   * </pre>
 
112
   *
 
113
   * If that sounds confusing your best bet is to simply forget about the
 
114
   * existance of this class and treat is as being identical to @ref UChar.
 
115
   */
 
116
  class UCharReference {
 
117
    friend class UString;
 
118
    UCharReference(UString *s, unsigned int off) : str(s), offset(off) { }
 
119
  public:
 
120
    /**
 
121
     * Set the referenced character to c.
 
122
     */
 
123
    UCharReference& operator=(UChar c);
 
124
    /**
 
125
     * Same operator as above except the argument that it takes.
 
126
     */
 
127
    UCharReference& operator=(char c) { return operator=(UChar(c)); }
 
128
    /**
 
129
     * @return Unicode value.
 
130
     */
 
131
    unsigned short unicode() const { return ref().unicode(); }
 
132
    /**
 
133
     * @return Lower byte.
 
134
     */
 
135
    unsigned char low() const { return ref().uc & 0xFF; }
 
136
    /**
 
137
     * @return Higher byte.
 
138
     */
 
139
    unsigned char high() const { return ref().uc >> 8; }
 
140
    /**
 
141
     * @return Character converted to lower case.
 
142
     */
 
143
    UChar toLower() const { return ref().toLower(); }
 
144
    /**
 
145
     * @return Character converted to upper case.
 
146
     */
 
147
    UChar toUpper() const  { return ref().toUpper(); }
 
148
  private:
 
149
    // not implemented, can only be constructed from UString
 
150
    UCharReference();
 
151
 
 
152
    UChar& ref() const;
 
153
    UString *str;
 
154
    int offset;
 
155
  };
 
156
 
 
157
  /**
 
158
   * @short 8 bit char based string class
 
159
   */
 
160
  class CString {
 
161
  public:
 
162
    CString() : data(0L) { }
 
163
    explicit CString(const char *c);
 
164
    CString(const CString &);
 
165
 
 
166
    ~CString();
 
167
 
 
168
    CString &append(const CString &);
 
169
    CString &operator=(const char *c);
 
170
    CString &operator=(const CString &);
 
171
    CString &operator+=(const CString &);
 
172
 
 
173
    int length() const;
 
174
    const char *c_str() const { return data; }
 
175
  private:
 
176
    char *data;
 
177
  };
 
178
 
 
179
  /**
 
180
   * @short Unicode string class
 
181
   */
 
182
  class UString {
 
183
    friend bool operator==(const UString&, const UString&);
 
184
    friend class UCharReference;
 
185
    friend class UConstString;
 
186
    /**
 
187
     * @internal
 
188
     */
 
189
    struct Rep {
 
190
      friend class UString;
 
191
      friend bool operator==(const UString&, const UString&);
 
192
      static Rep *create(UChar *d, int l);
 
193
      inline UChar *data() const { return dat; }
 
194
      inline int length() const { return len; }
 
195
 
 
196
      inline void ref() { rc++; }
 
197
      inline int deref() { return --rc; }
 
198
 
 
199
      UChar *dat;
 
200
      int len;
 
201
      int rc;
 
202
      static Rep null;
 
203
    };
 
204
 
 
205
  public:
 
206
    /**
 
207
     * Constructs a null string.
 
208
     */
 
209
    UString();
 
210
    /**
 
211
     * Constructs a string from the single character c.
 
212
     */
 
213
    explicit UString(char c);
 
214
    /**
 
215
     * Constructs a string from the single character c.
 
216
     */
 
217
    explicit UString(UChar c);
 
218
    /**
 
219
     * Constructs a string from a classical zero determined char string.
 
220
     */
 
221
    explicit UString(const char *c);
 
222
    /**
 
223
     * Constructs a string from an array of Unicode characters of the specified
 
224
     * length.
 
225
     */
 
226
    UString(const UChar *c, int length);
 
227
    /**
 
228
     * If copy is false a shallow copy of the string will be created. That
 
229
     * means that the data will NOT be copied and you'll have to guarantee that
 
230
     * it doesn't get deleted during the lifetime of the UString object.
 
231
     */
 
232
    UString(UChar *c, int length, bool copy);
 
233
    /**
 
234
     * Copy constructor. Makes a shallow copy only.
 
235
     */
 
236
    UString(const UString &);
 
237
    /**
 
238
     * Destructor. If this handle was the only one holding a reference to the
 
239
     * string the data will be freed.
 
240
     */
 
241
    ~UString();
 
242
 
 
243
    /**
 
244
     * Constructs a string from an int.
 
245
     */
 
246
    static UString from(int i);
 
247
    /**
 
248
     * Constructs a string from an unsigned int.
 
249
     */
 
250
    static UString from(unsigned int u);
 
251
    /**
 
252
     * Constructs a string from a double.
 
253
     */
 
254
    static UString from(double d);
 
255
 
 
256
    /**
 
257
     * Append another string.
 
258
     */
 
259
    UString &append(const UString &);
 
260
 
 
261
    /**
 
262
     * @return The string converted to the 8-bit string type @ref CString().
 
263
     */
 
264
    CString cstring() const;
 
265
    /**
 
266
     * Convert the Unicode string to plain ASCII chars chopping of any higher
 
267
     * bytes. This method should only be used for *debugging* purposes as it
 
268
     * is neither Unicode safe nor free from side effects. In order not to
 
269
     * waste any memory the char buffer is static and *shared* by all UString
 
270
     * instances.
 
271
     */
 
272
    char *ascii() const;
 
273
 
 
274
    /**
 
275
     * Assignment operator.
 
276
     */
 
277
    UString &operator=(const char *c);
 
278
    /**
 
279
     * Assignment operator.
 
280
     */
 
281
    UString &operator=(const UString &);
 
282
    /**
 
283
     * Appends the specified string.
 
284
     */
 
285
    UString &operator+=(const UString &s);
 
286
 
 
287
    /**
 
288
     * @return A pointer to the internal Unicode data.
 
289
     */
 
290
    const UChar* data() const { return rep->data(); }
 
291
    /**
 
292
     * @return True if null.
 
293
     */
 
294
    bool isNull() const { return (rep == &Rep::null); }
 
295
    /**
 
296
     * @return True if null or zero length.
 
297
     */
 
298
    bool isEmpty() const { return (!rep->len); }
 
299
    /**
 
300
     * Use this if you want to make sure that this string is a plain ASCII
 
301
     * string. For example, if you don't want to lose any information when
 
302
     * using @ref cstring() or @ref ascii().
 
303
     *
 
304
     * @return True if the string doesn't contain any non-ASCII characters.
 
305
     */
 
306
    bool is8Bit() const;
 
307
    /**
 
308
     * @return The length of the string.
 
309
     */
 
310
    int length() const { return rep->length(); }
 
311
    /**
 
312
     * Const character at specified position.
 
313
     */
 
314
    UChar operator[](int pos) const;
 
315
    /**
 
316
     * Writable reference to character at specified position.
 
317
     */
 
318
    UCharReference operator[](int pos);
 
319
 
 
320
    /**
 
321
     * Attempts an conversion to a number. Apart from floating point numbers,
 
322
     * the algorithm will recognize hexadecimal representations (as
 
323
     * indicated by a 0x or 0X prefix) and +/- Infinity.
 
324
     * Returns NaN if the conversion failed.
 
325
     * @param tolerant if true, toDouble can tolerate garbage after the number.
 
326
     */
 
327
    double toDouble(bool tolerant=false) const;
 
328
    /**
 
329
     * Attempts an conversion to an unsigned long integer. ok will be set
 
330
     * according to the success.
 
331
     */
 
332
    unsigned long toULong(bool *ok = 0L) const;
 
333
    /**
 
334
     * @return Position of first occurence of f starting at position pos.
 
335
     * -1 if the search was not successful.
 
336
     */
 
337
    int find(const UString &f, int pos = 0) const;
 
338
    /**
 
339
     * @return Position of first occurence of f searching backwards from
 
340
     * position pos.
 
341
     * -1 if the search was not successful.
 
342
     */
 
343
    int rfind(const UString &f, int pos) const;
 
344
    /**
 
345
     * @return The sub string starting at position pos and length len.
 
346
     */
 
347
    UString substr(int pos = 0, int len = -1) const;
 
348
    /**
 
349
     * Static instance of a null string.
 
350
     */
 
351
    static UString null;
 
352
 
 
353
  private:
 
354
    void attach(Rep *r);
 
355
    void detach();
 
356
    void release();
 
357
    Rep *rep;
 
358
  };
 
359
 
 
360
  inline bool operator==(const UChar &c1, const UChar &c2) {
 
361
    return (c1.uc == c2.uc);
 
362
  }
 
363
  inline bool operator!=(const UChar &c1, const UChar &c2) {
 
364
    return !(c1 == c2);
 
365
  }
 
366
  bool operator==(const UString& s1, const UString& s2);
 
367
  inline bool operator!=(const UString& s1, const UString& s2) {
 
368
    return !Sidewinder::operator==(s1, s2);
 
369
  }
 
370
  bool operator<(const UString& s1, const UString& s2);
 
371
  bool operator==(const UString& s1, const char *s2);
 
372
  inline bool operator!=(const UString& s1, const char *s2) {
 
373
    return !Sidewinder::operator==(s1, s2);
 
374
  }
 
375
  inline bool operator==(const char *s1, const UString& s2) {
 
376
    return operator==(s2, s1);
 
377
  }
 
378
  inline bool operator!=(const char *s1, const UString& s2) {
 
379
    return !Sidewinder::operator==(s1, s2);
 
380
  }
 
381
  bool operator==(const CString& s1, const CString& s2);
 
382
  UString operator+(const UString& s1, const UString& s2);
 
383
 
 
384
 
 
385
  class UConstString : private UString {
 
386
    public:
 
387
      UConstString( UChar* data, unsigned int length );
 
388
      ~UConstString();
 
389
 
 
390
      const UString& string() const { return *this; }
 
391
  };
 
392
 
 
393
} // namespace SIDEWINDER_USTRING_H
 
394
 
 
395
#endif