summaryrefslogtreecommitdiffstats
path: root/src/ZYString.h
blob: ebb84388b48e5d9f439f88a912e48755a3255708 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* vim:set et ts=4 sts=4:
 *
 * ibus-libzhuyin - New Zhuyin engine based on libzhuyin for IBus
 *
 * Copyright (c) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
#ifndef __ZY_STRING_H_
#define __ZY_STRING_H_

#include <glib.h>
#include <stdarg.h>
#include <assert.h>
#include <string>

namespace ZY {

class String : public std::string {
public:
    String () : std::string () { }
    String (const gchar *str) : std::string (str) { }
    String (const std::string &str) : std::string (str) { }
    /* TODO: remove the following line later. */
    G_DEPRECATED String (gint len) : std::string () { reserve (len); }
    String (const gchar ch) : std::string (1, ch) { }

    String & printf (const gchar *fmt, ...)
    {
        gchar *str;
        va_list args;

        va_start (args, fmt);
        str = g_strdup_vprintf (fmt, args);
        va_end (args);

        assign (str);
        g_free (str);
        return *this;
    }

    String & appendPrintf (const gchar *fmt, ...)
    {
        gchar *str;
        va_list args;

        va_start (args, fmt);
        str = g_strdup_vprintf (fmt, args);
        va_end (args);

        append (str);
        g_free (str);

        return *this;
    }

    String & appendUnichar (gunichar ch)
    {
        gchar str[12];
        gint len;
        len = g_unichar_to_utf8 (ch, str);
        str[len] = 0;
        append (str);
        return *this;
    }

    String & insert (gint i, gchar ch)
    {
        std::string::insert (i, 1, ch);
        return *this;
    }

    String & truncate (guint len)
    {
        erase(len);
        return *this;
    }

    gsize utf8Length (void) const
    {
        return g_utf8_strlen (c_str(), -1);
    }

    String & operator<< (gint i)
    {
        return appendPrintf ("%d", i);
    }

    String & operator<< (guint i)
    {
        return appendPrintf ("%u", i);
    }

    String & operator<< (const gchar ch)
    {
        append (1, ch);
        return *this;
    }

    String & operator<< (const gchar *str)
    {
        append (str);
        return *this;
    }

    String & operator<< (const gunichar *wstr)
    {
        gchar *str;
        GError *error;
        str = g_ucs4_to_utf8 (wstr, -1, NULL, NULL, &error);
        if (str == NULL) {
            g_warning ("convert ucs4 to utf8 failed: %s", error->message);
            g_error_free (error);
        }
        else {
            append (str);
            g_free (str);
        }
        return *this;
    }

    gchar operator[] (gint i)
    {
        return std::string::operator[] (i);
    }

    String & operator<< (const std::string &str)
    {
        return operator<< (str.c_str ());
    }

    String & operator<< (const String &str)
    {
        return operator<< ((const gchar *)str);
    }

    String & operator= (const gchar * str)
    {
        assign (str);
        return *this;
    }

    String & operator= (const gchar ch)
    {
        assign (1, ch);
        return *this;
    }

    operator const gchar *(void) const
    {
        return this->c_str ();
    }

    operator gboolean (void) const
    {
        return ! empty ();
    }
};

};
#endif