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
15 * Szabo, Janos Zoltan – initial implementation
16 * Zalanyi, Balazs Andor
18 ******************************************************************************/
19 #ifndef _Common_ustring_HH
20 #define _Common_ustring_HH
27 * Class for handling universal (multi-byte) string values.
31 struct universal_char {
32 unsigned char group, plane, row, cell;
35 /** string value structure */
36 struct ustring_struct {
37 unsigned int ref_count;
39 universal_char uchars_ptr[1];
42 ustring(size_t n) : val_ptr(0) { init_struct(n); }
44 void init_struct(size_t n_uchars);
45 void enlarge_memory(size_t incr);
49 /** Three-way lexicographical comparison of \a *this and \a s, much
51 int compare(const ustring& s) const;
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;
59 /** Constructs an empty string. */
60 ustring() : val_ptr(0) { init_struct(0); }
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);
66 /** Constructs from an array of \a n quadruples starting at \a uc_ptr. */
67 ustring(size_t n, const universal_char *uc_ptr);
69 /** Constructs a universal string from \a s. */
70 ustring(const string& s);
72 /** Copy constructor */
73 ustring(const ustring& s) : val_ptr(s.val_ptr) { val_ptr->ref_count++;}
75 /** The destructor. */
76 ~ustring() { clean_up(); }
78 /** Returns the size of the string. */
79 inline size_t size() const { return val_ptr->n_uchars; }
81 /** true if the string's size is 0. */
82 inline bool empty() const { return val_ptr->n_uchars == 0; }
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; }
89 /** Erases the string. */
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;
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);
102 /** Returns the printable representation of value stored in \a this.
103 * The non-printable and special characters are escaped in the returned
105 string get_stringRepr() const;
107 /** Returns the charstring representation of value stored in \a this.
108 * quadruples will be converted into the form \q{group,plane,row,cell}
110 string get_stringRepr_for_pattern() const;
112 /** Converts the value to hex form used by match and regexp in case of
113 * universal charstring parameters.
115 char* convert_to_regexp_form() const;
117 /** Assignment operator. */
118 ustring& operator=(const ustring&);
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);
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;
130 /** String concatenation. */
131 ustring operator+(const string& s2) const;
133 /** String concatenation. */
134 ustring operator+(const ustring& s2) const;
136 /** Append \a s to \a *this. */
137 ustring& operator+=(const string& s);
139 /** Append \a s to \a *this. */
140 ustring& operator+=(const ustring& s);
142 /** String equality. */
143 bool operator==(const ustring& s2) const;
145 /** String inequality. */
146 inline bool operator!=(const ustring& s2) const { return !(*this == s2); }
148 /** String comparison. */
149 inline bool operator<=(const ustring& s) const { return compare(s) <= 0; }
151 /** String comparison. */
152 inline bool operator<(const ustring& s) const { return compare(s) < 0; }
154 /** String comparison. */
155 inline bool operator>=(const ustring& s) const { return compare(s) >= 0; }
157 /** String comparison. */
158 inline bool operator>(const ustring& s) const { return compare(s) > 0; }
162 extern bool operator==(const ustring::universal_char& uc1,
163 const ustring::universal_char& uc2);
165 extern bool operator<(const ustring::universal_char& uc1,
166 const ustring::universal_char& uc2);
168 /** Converts the unicode string to UTF-8 format */
169 extern string ustring_to_uft8(const ustring&);
171 #endif // _Common_ustring_HH