Merge pull request #28 from balaskoa/master
[deliverable/titan.core.git] / compiler2 / ustring.hh
1 /******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Balasko, Jeno
10 * Baranyi, Botond
11 * Forstner, Matyas
12 * Kovacs, Ferenc
13 * Raduly, Csaba
14 * Szabados, Kristof
15 * Szabo, Janos Zoltan – initial implementation
16 * Zalanyi, Balazs Andor
17 *
18 ******************************************************************************/
19 #ifndef _Common_ustring_HH
20 #define _Common_ustring_HH
21
22 #include <string.h>
23
24 class string;
25
26 /**
27 * Class for handling universal (multi-byte) string values.
28 */
29 class ustring {
30 public:
31 struct universal_char {
32 unsigned char group, plane, row, cell;
33 };
34 private:
35 /** string value structure */
36 struct ustring_struct {
37 unsigned int ref_count;
38 size_t n_uchars;
39 universal_char uchars_ptr[1];
40 } *val_ptr;
41
42 ustring(size_t n) : val_ptr(0) { init_struct(n); }
43
44 void init_struct(size_t n_uchars);
45 void enlarge_memory(size_t incr);
46 void copy_value();
47 void clean_up();
48
49 /** Three-way lexicographical comparison of \a *this and \a s, much
50 * like \c strcmp. */
51 int compare(const ustring& s) const;
52
53 public:
54
55 /** The largest possible value of type size_t. That is, size_t(-1). */
56 static const size_t max_string_len =
57 (-1 - sizeof(ustring_struct)) / sizeof(universal_char) + 1;
58
59 /** Constructs an empty string. */
60 ustring() : val_ptr(0) { init_struct(0); }
61
62 /** Constructs a single quadruple from the given values. */
63 ustring(unsigned char p_group, unsigned char p_plane, unsigned char p_row,
64 unsigned char p_cell);
65
66 /** Constructs from an array of \a n quadruples starting at \a uc_ptr. */
67 ustring(size_t n, const universal_char *uc_ptr);
68
69 /** Constructs a universal string from \a s. */
70 ustring(const string& s);
71
72 /** Copy constructor */
73 ustring(const ustring& s) : val_ptr(s.val_ptr) { val_ptr->ref_count++;}
74
75 /** The destructor. */
76 ~ustring() { clean_up(); }
77
78 /** Returns the size of the string. */
79 inline size_t size() const { return val_ptr->n_uchars; }
80
81 /** true if the string's size is 0. */
82 inline bool empty() const { return val_ptr->n_uchars == 0; }
83
84 /** Returns a pointer to an array of quadruples
85 * representing the string's contents.
86 * The array contains \a size() elements without any terminating symbol. */
87 inline const universal_char *u_str() const { return val_ptr->uchars_ptr; }
88
89 /** Erases the string. */
90 void clear();
91
92 /** Creates a string from a substring of \a *this.
93 * The substring begins at character position \a pos,
94 * and terminates at character position \a pos+n or at the end of
95 * the string, whichever comes first. */
96 ustring substr(size_t pos=0, size_t n=max_string_len) const;
97
98 /** Replaces the \a n long substring of \a *this beginning
99 * at position \a pos with the string \a s. */
100 void replace(size_t pos, size_t n, const ustring& s);
101
102 /** Returns the printable representation of value stored in \a this.
103 * The non-printable and special characters are escaped in the returned
104 * string. */
105 string get_stringRepr() const;
106
107 /** Returns the charstring representation of value stored in \a this.
108 * quadruples will be converted into the form \q{group,plane,row,cell}
109 */
110 string get_stringRepr_for_pattern() const;
111
112 /** Converts the value to hex form used by match and regexp in case of
113 * universal charstring parameters.
114 */
115 char* convert_to_regexp_form() const;
116
117 /** Assignment operator. */
118 ustring& operator=(const ustring&);
119
120 /** Returns the <em>n</em>th character.
121 * The first character's position is zero.
122 * If \a n >= size() then... Fatal error. */
123 universal_char& operator[](size_t n);
124
125 /** Returns the <em>n</em>th character.
126 * The first character's position is zero.
127 * If \a n >= size() then... Fatal error. */
128 const universal_char& operator[](size_t n) const;
129
130 /** String concatenation. */
131 ustring operator+(const string& s2) const;
132
133 /** String concatenation. */
134 ustring operator+(const ustring& s2) const;
135
136 /** Append \a s to \a *this. */
137 ustring& operator+=(const string& s);
138
139 /** Append \a s to \a *this. */
140 ustring& operator+=(const ustring& s);
141
142 /** String equality. */
143 bool operator==(const ustring& s2) const;
144
145 /** String inequality. */
146 inline bool operator!=(const ustring& s2) const { return !(*this == s2); }
147
148 /** String comparison. */
149 inline bool operator<=(const ustring& s) const { return compare(s) <= 0; }
150
151 /** String comparison. */
152 inline bool operator<(const ustring& s) const { return compare(s) < 0; }
153
154 /** String comparison. */
155 inline bool operator>=(const ustring& s) const { return compare(s) >= 0; }
156
157 /** String comparison. */
158 inline bool operator>(const ustring& s) const { return compare(s) > 0; }
159
160 };
161
162 extern bool operator==(const ustring::universal_char& uc1,
163 const ustring::universal_char& uc2);
164
165 extern bool operator<(const ustring::universal_char& uc1,
166 const ustring::universal_char& uc2);
167
168 /** Converts the unicode string to UTF-8 format */
169 extern string ustring_to_uft8(const ustring&);
170
171 #endif // _Common_ustring_HH
This page took 0.033757 seconds and 5 git commands to generate.