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