1 /* vim:set et ts=4 sts=4: 2 * 3 * libpyzy - The Chinese PinYin and Bopomofo conversion library. 4 * 5 * Copyright (c) 2008-2010 Peng Huang <shawn.p.huang@gmail.com> 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 20 * USA 21 */ 22 #ifndef __PYZY_STRING_H_ 23 #define __PYZY_STRING_H_ 24 25 #include <glib.h> 26 #include <stdarg.h> 27 #include <string> 28 29 #include "Util.h" 30 31 namespace PyZy { 32 33 class String : public std::string { 34 public: String()35 String () : std::string () { } String(const char * str)36 String (const char *str) : std::string (str) { } String(const std::string & str)37 String (const std::string &str) : std::string (str) { } String(size_t len)38 String (size_t len) : std::string () { reserve (len); } 39 printf(const char * fmt,...)40 String & printf (const char *fmt, ...) 41 { 42 char *str; 43 va_list args; 44 45 va_start (args, fmt); 46 str = g_strdup_vprintf (fmt, args); 47 va_end (args); 48 49 assign (str); 50 g_free (str); 51 return *this; 52 } 53 appendPrintf(const char * fmt,...)54 String & appendPrintf (const char *fmt, ...) 55 { 56 char *str; 57 va_list args; 58 59 va_start (args, fmt); 60 str = g_strdup_vprintf (fmt, args); 61 va_end (args); 62 63 append (str); 64 g_free (str); 65 66 return *this; 67 } 68 appendUnichar(unichar ch)69 String & appendUnichar (unichar ch) 70 { 71 char str[12]; 72 size_t len; 73 len = g_unichar_to_utf8 (ch, str); 74 str[len] = 0; 75 append (str); 76 return *this; 77 } 78 insert(size_t i,char ch)79 String & insert (size_t i, char ch) 80 { 81 std::string::insert (i, 1, ch); 82 return *this; 83 } 84 truncate(size_t len)85 String & truncate (size_t len) 86 { 87 erase(len); 88 return *this; 89 } 90 replace(const char * pattern,const char * str)91 String & replace (const char *pattern, const char *str) 92 { 93 String result; 94 String::size_type pos = 0; 95 String::size_type pos_before = 0; 96 const String::size_type length = std::string (pattern).size (); 97 98 while ((pos = this->find (pattern, pos)) != String::npos) { 99 result.append (*this, pos_before, pos - pos_before); 100 result.append (str); 101 pos += length; 102 pos_before = pos; 103 } 104 result.append (*this, pos_before, this->size () - pos_before); 105 this->assign (result); 106 return *this; 107 } 108 utf8Length(void)109 size_t utf8Length (void) const 110 { 111 return g_utf8_strlen (c_str(), -1); 112 } 113 114 String & operator<< (int i) 115 { 116 return appendPrintf ("%d", i); 117 } 118 119 String & operator<< (unsigned int i) 120 { 121 return appendPrintf ("%u", i); 122 } 123 124 String & operator<< (unsigned long i) 125 { 126 return appendPrintf ("%lu", i); 127 } 128 129 String & operator<< (const char ch) 130 { 131 append (1, ch); 132 return *this; 133 } 134 135 String & operator<< (const char *str) 136 { 137 append (str); 138 return *this; 139 } 140 141 String & operator<< (const unichar *wstr) 142 { 143 char *str; 144 GError *error; 145 str = g_ucs4_to_utf8 (wstr, -1, NULL, NULL, &error); 146 if (str == NULL) { 147 g_warning ("convert ucs4 to utf8 failed: %s", error->message); 148 g_error_free (error); 149 } 150 else { 151 append (str); 152 g_free (str); 153 } 154 return *this; 155 } 156 157 char operator[] (size_t i) 158 { 159 return std::string::operator[] (i); 160 } 161 162 String & operator<< (const std::string &str) 163 { 164 return operator<< (str.c_str ()); 165 } 166 167 String & operator<< (const String &str) 168 { 169 return operator<< ((const char *)str); 170 } 171 172 String & operator= (const char * str) 173 { 174 assign (str); 175 return *this; 176 } 177 178 operator const char *(void) const 179 { 180 return this->c_str (); 181 } 182 183 operator bool (void) const 184 { 185 return ! empty (); 186 } 187 }; 188 189 }; // namespace PyZy 190 191 #endif // __PYZY_STRING_H_ 192