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