1 /***************************************************************************
2  *   Copyright (C) 2012~2012 by CSSlayer                                   *
3  *   wengxt@gmail.com                                                      *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.              *
19  ***************************************************************************/
20 
21 #include "ustring.h"
22 #include <fcitx-utils/utarray.h>
23 #include <fcitx-utils/utf8.h>
24 
25 static const UT_icd ucs_icd = { sizeof(ucschar), NULL, NULL, NULL};
26 
27 UString*
ustring_new()28 ustring_new()
29 {
30     UString* str;
31     utarray_new(str, &ucs_icd);
32     return str;
33 }
34 
35 UString*
ustring_dup(const UString * str)36 ustring_dup(const UString* str)
37 {
38     UString* dup;
39     dup = ustring_new();
40     ustring_append(dup, str);
41     return dup;
42 }
43 
44 void
ustring_delete(UString * str)45 ustring_delete(UString* str)
46 {
47     utarray_free(str);
48 }
49 
50 void
ustring_clear(UString * str)51 ustring_clear(UString* str)
52 {
53     utarray_clear(str);
54 }
55 
56 UString*
ustring_erase(UString * str,size_t pos,size_t len)57 ustring_erase(UString* str, size_t pos, size_t len)
58 {
59     if (len > 0)
60         utarray_erase(str, pos, len);
61     return str;
62 }
63 
64 ucschar*
ustring_begin(UString * str)65 ustring_begin(UString* str)
66 {
67     return (ucschar*) utarray_front(str);
68 }
69 
70 ucschar*
ustring_end(UString * str)71 ustring_end(UString* str)
72 {
73     return (ucschar*) utarray_eltptr(str, utarray_len(str));
74 }
75 
76 unsigned
ustring_length(const UString * str)77 ustring_length(const UString* str)
78 {
79     return utarray_len(str);
80 }
81 
82 UString*
ustring_append(UString * str,const UString * s)83 ustring_append(UString* str, const UString* s)
84 {
85     utarray_concat(str, s);
86     return str;
87 }
88 
89 UString*
ustring_append_ucs4(UString * str,const ucschar * s)90 ustring_append_ucs4(UString* str, const ucschar* s)
91 {
92     const ucschar*p = s;
93     while (*p != 0) {
94         utarray_push_back(str, p);
95         p++;
96     }
97 
98     return str;
99 }
100 
101 UString*
ustring_append_utf8(UString * str,const char * utf8)102 ustring_append_utf8(UString* str, const char* utf8)
103 {
104     while (*utf8 != '\0') {
105         ucschar c;
106         utf8 = fcitx_utf8_get_char(utf8, (uint32_t*) &c);
107         utarray_push_back(str, &c);
108     }
109     return str;
110 }
111