unicode 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/ext/ustring.c ADDED
@@ -0,0 +1,206 @@
1
+ /*
2
+ * Simple string library
3
+ * Version 0.2
4
+ * 1999 by yoshidam
5
+ */
6
+
7
+ #include <stdio.h>
8
+ #include "ustring.h"
9
+
10
+ UString*
11
+ UStr_alloc(UString* str)
12
+ {
13
+ str->size = USTR_INITIAL_STRING_LEN;
14
+ str->len = 0;
15
+ if ((str->str = (unsigned char*)malloc(USTR_INITIAL_STRING_LEN)) == NULL) {
16
+ str->size = 0;
17
+ return NULL;
18
+ }
19
+
20
+ return str;
21
+ }
22
+
23
+ UString*
24
+ UStr_enlarge(UString* str, int size)
25
+ {
26
+ unsigned char* newptr;
27
+
28
+ if ((newptr = (unsigned char*)realloc(str->str, str->size + size))
29
+ == NULL) {
30
+ return NULL;
31
+ }
32
+ str->str = newptr;
33
+ str->size += size;
34
+
35
+ return str;
36
+ }
37
+
38
+ void
39
+ UStr_free(UString* str)
40
+ {
41
+ str->size = 0;
42
+ str->len = 0;
43
+ free(str->str);
44
+ }
45
+
46
+ int
47
+ UStr_addChars(UString* s, const unsigned char* a, int len)
48
+ {
49
+ if (s->len + len >= s->size) {
50
+ UStr_enlarge(s, len + USTR_STRING_EXTEND_LEN);
51
+ }
52
+ memcpy(s->str + s->len, a, len);
53
+ s->len += len;
54
+
55
+ return s->len;
56
+ }
57
+
58
+ int
59
+ UStr_addChar(UString* s, unsigned char a)
60
+ {
61
+ if (s->len + 1 >= s->size) {
62
+ UStr_enlarge(s, USTR_STRING_EXTEND_LEN);
63
+ }
64
+ *(s->str + s->len) = a;
65
+ (s->len)++;
66
+
67
+ return s->len;
68
+ }
69
+
70
+ int
71
+ UStr_addChar2(UString* s, unsigned char a1, unsigned char a2)
72
+ {
73
+ if (s->len + 2 >= s->size) {
74
+ UStr_enlarge(s, USTR_STRING_EXTEND_LEN);
75
+ }
76
+ *(s->str + s->len) = a1;
77
+ *(s->str + s->len + 1) = a2;
78
+ s->len += 2;
79
+
80
+ return s->len;
81
+ }
82
+
83
+ int
84
+ UStr_addChar3(UString* s, unsigned char a1, unsigned char a2, unsigned char a3)
85
+ {
86
+ if (s->len + 3 >= s->size) {
87
+ UStr_enlarge(s, USTR_STRING_EXTEND_LEN);
88
+ }
89
+ *(s->str + s->len) = a1;
90
+ *(s->str + s->len + 1) = a2;
91
+ *(s->str + s->len + 2) = a3;
92
+ s->len += 3;
93
+
94
+ return s->len;
95
+ }
96
+
97
+ int
98
+ UStr_addChar4(UString* s, unsigned char a1, unsigned char a2,
99
+ unsigned char a3, unsigned char a4)
100
+ {
101
+ if (s->len + 4 >= s->size) {
102
+ UStr_enlarge(s, USTR_STRING_EXTEND_LEN);
103
+ }
104
+ *(s->str + s->len) = a1;
105
+ *(s->str + s->len + 1) = a2;
106
+ *(s->str + s->len + 2) = a3;
107
+ *(s->str + s->len + 3) = a4;
108
+ s->len += 4;
109
+
110
+ return s->len;
111
+ }
112
+
113
+ int
114
+ UStr_addChar5(UString* s, unsigned char a1, unsigned char a2,
115
+ unsigned char a3, unsigned char a4, unsigned char a5)
116
+ {
117
+ if (s->len + 5 >= s->size) {
118
+ UStr_enlarge(s, USTR_STRING_EXTEND_LEN);
119
+ }
120
+ *(s->str + s->len) = a1;
121
+ *(s->str + s->len + 1) = a2;
122
+ *(s->str + s->len + 2) = a3;
123
+ *(s->str + s->len + 3) = a4;
124
+ *(s->str + s->len + 4) = a5;
125
+ s->len += 5;
126
+
127
+ return s->len;
128
+ }
129
+
130
+ int
131
+ UStr_addChar6(UString* s, unsigned char a1, unsigned char a2,
132
+ unsigned char a3, unsigned char a4,
133
+ unsigned char a5, unsigned char a6)
134
+ {
135
+ if (s->len + 6 >= s->size) {
136
+ UStr_enlarge(s, USTR_STRING_EXTEND_LEN);
137
+ }
138
+ *(s->str + s->len) = a1;
139
+ *(s->str + s->len + 1) = a2;
140
+ *(s->str + s->len + 2) = a3;
141
+ *(s->str + s->len + 3) = a4;
142
+ *(s->str + s->len + 4) = a5;
143
+ *(s->str + s->len + 5) = a6;
144
+ s->len += 6;
145
+
146
+ return s->len;
147
+ }
148
+
149
+ int
150
+ UStr_addWChar(UString* ustr, int c)
151
+ {
152
+ if (c < 128) { /* 0x0000-0x00FF */
153
+ UStr_addChar(ustr, c);
154
+ }
155
+ else if (c < 2048) { /* 0x0100-0x07FF */
156
+ unsigned char b2 = c & 63;
157
+ unsigned char b1 = c >> 6;
158
+ UStr_addChar2(ustr, b1 | 192, b2 | 128);
159
+
160
+ }
161
+ else if (c < 0x10000) { /* 0x0800-0xFFFF */
162
+ unsigned char b3 = c & 63;
163
+ unsigned char b2 = (c >> 6) & 63;
164
+ unsigned char b1 = c >> 12;
165
+ UStr_addChar3(ustr, b1 | 224, b2 | 128, b3 | 128);
166
+ }
167
+ else if (c < 0x200000) { /* 0x00010000-0x001FFFFF */
168
+ unsigned char b4 = c & 63;
169
+ unsigned char b3 = (c >> 6) & 63;
170
+ unsigned char b2 = (c >> 12) & 63;
171
+ unsigned char b1 = c >> 18;
172
+ UStr_addChar4(ustr, b1 | 240, b2 | 128, b3 | 128, b4 | 128);
173
+ }
174
+ else if (c < 0x4000000) { /* 0x00200000-0x03FFFFFF */
175
+ unsigned char b5 = c & 63;
176
+ unsigned char b4 = (c >> 6) & 63;
177
+ unsigned char b3 = (c >> 12) & 63;
178
+ unsigned char b2 = (c >> 18) & 63;
179
+ unsigned char b1 = c >> 24;
180
+ UStr_addChar5(ustr, b1 | 248, b2 | 128, b3 | 128, b4 | 128, b5 | 128);
181
+ }
182
+ else if (c < 0x80000000) { /* 0x04000000-0x7FFFFFFF */
183
+ unsigned char b6 = c & 63;
184
+ unsigned char b5 = (c >> 6) & 63;
185
+ unsigned char b4 = (c >> 12) & 63;
186
+ unsigned char b3 = (c >> 18) & 63;
187
+ unsigned char b2 = (c >> 24) & 63;
188
+ unsigned char b1 = (c >> 30) & 63;
189
+ UStr_addChar6(ustr, b1 | 252, b2 | 128, b3 | 128,
190
+ b4 | 128, b5 | 128, b6 | 128);
191
+ }
192
+
193
+ return ustr->len;
194
+ }
195
+
196
+ void
197
+ UStr_dump(UString* s)
198
+ {
199
+ int i;
200
+
201
+ printf("[%d/%d] ", s->len, s->size);
202
+ for (i = 0; i < s->len ; i++) {
203
+ printf("%02x ", *(s->str + i));
204
+ }
205
+ printf("\n");
206
+ }
data/ext/ustring.h ADDED
@@ -0,0 +1,48 @@
1
+ /*
2
+ * Simple string library
3
+ * Version 0.2
4
+ * 1999 by yoshidam
5
+ */
6
+
7
+ #ifndef _USTRING_H
8
+ #define _USTRING_H
9
+
10
+ #ifdef __cplusplus
11
+ extern "C" {
12
+ #endif
13
+
14
+ #define USTR_INITIAL_STRING_LEN 1024
15
+ #define USTR_STRING_EXTEND_LEN 1024
16
+
17
+ /*#define malloc(s) xmalloc(s)*/
18
+ /*#define relloc(p, s) xrelloc(p, s)*/
19
+
20
+ typedef struct _UString {
21
+ unsigned char* str;
22
+ int len;
23
+ int size;
24
+ } UString;
25
+
26
+ UString* UStr_alloc(UString* str);
27
+ UString* UStr_enlarge(UString* str, int size);
28
+ void UStr_free(UString* str);
29
+ int UStr_addUhars(UString* s, const unsigned char* a, int len);
30
+ int UStr_addChar(UString* s, unsigned char a);
31
+ int UStr_addChar2(UString* s, unsigned char a1, unsigned char a2);
32
+ int UStr_addChar3(UString* s, unsigned char a1, unsigned char a2,
33
+ unsigned char a3);
34
+ int UStr_addChar4(UString* s, unsigned char a1, unsigned char a2,
35
+ unsigned char a3, unsigned char a4);
36
+ int UStr_addChar5(UString* s, unsigned char a1, unsigned char a2,
37
+ unsigned char a3, unsigned char a4, unsigned char a5);
38
+ int UStr_addChar6(UString* s, unsigned char a1, unsigned char a2,
39
+ unsigned char a3, unsigned char a4,
40
+ unsigned char a5, unsigned char a6);
41
+ int UStr_addWChar(UString* s, int c);
42
+ void UStr_dump(UString* s);
43
+
44
+ #ifdef __cplusplus
45
+ }
46
+ #endif
47
+
48
+ #endif
data/ext/wstring.c ADDED
@@ -0,0 +1,185 @@
1
+ /*
2
+ * Simple wide string Library
3
+ * Version 0.1
4
+ * 1999 by yoshidam
5
+ */
6
+
7
+ #include <stdio.h>
8
+ #include "wstring.h"
9
+
10
+ WString*
11
+ WStr_alloc(WString* str)
12
+ {
13
+ str->size = WSTR_INITIAL_STRING_LEN;
14
+ str->len = 0;
15
+ if ((str->str =
16
+ (int*)malloc(WSTR_INITIAL_STRING_LEN * sizeof(int))) == NULL) {
17
+ str->size = 0;
18
+ return NULL;
19
+ }
20
+
21
+ return str;
22
+ }
23
+
24
+ WString*
25
+ WStr_enlarge(WString* str, int size)
26
+ {
27
+ int* newptr;
28
+
29
+ if ((newptr = (int*)realloc(str->str, (str->size + size) * sizeof(int)))
30
+ == NULL) {
31
+ return NULL;
32
+ }
33
+ str->str = newptr;
34
+ str->size += size;
35
+
36
+ return str;
37
+ }
38
+
39
+ void
40
+ WStr_free(WString* str)
41
+ {
42
+ str->size = 0;
43
+ str->len = 0;
44
+ free(str->str);
45
+ }
46
+
47
+ int
48
+ WStr_addWChars(WString* s, const int* a, int len)
49
+ {
50
+ if (s->len + len >= s->size) {
51
+ WStr_enlarge(s, len + WSTR_STRING_EXTEND_LEN);
52
+ }
53
+ memcpy(s->str + s->len, a, len * sizeof(int));
54
+ s->len += len;
55
+
56
+ return s->len;
57
+ }
58
+
59
+ int
60
+ WStr_addWChar(WString* s, int a)
61
+ {
62
+ if (s->len + 1 >= s->size) {
63
+ WStr_enlarge(s, WSTR_STRING_EXTEND_LEN);
64
+ }
65
+ *(s->str + s->len) = a;
66
+ (s->len)++;
67
+
68
+ return s->len;
69
+ }
70
+
71
+ int
72
+ WStr_pushWString(WString* s, const WString* add)
73
+ {
74
+ if (s->len + add->len >= s->size) {
75
+ WStr_enlarge(s, add->len + WSTR_STRING_EXTEND_LEN);
76
+ }
77
+ memcpy(s->str + s->len, add->str, add->len * sizeof(int));
78
+ s->len += add->len;
79
+
80
+ return s->len;
81
+ }
82
+
83
+ int
84
+ WStr_addWChar2(WString* s, int a1, int a2)
85
+ {
86
+ if (s->len + 2 >= s->size) {
87
+ WStr_enlarge(s, WSTR_STRING_EXTEND_LEN);
88
+ }
89
+ *(s->str + s->len) = a1;
90
+ *(s->str + s->len + 1) = a2;
91
+ s->len += 2;
92
+
93
+ return s->len;
94
+ }
95
+
96
+ int
97
+ WStr_addWChar3(WString* s, int a1, int a2, int a3)
98
+ {
99
+ if (s->len + 3 >= s->size) {
100
+ WStr_enlarge(s, WSTR_STRING_EXTEND_LEN);
101
+ }
102
+ *(s->str + s->len) = a1;
103
+ *(s->str + s->len + 1) = a2;
104
+ *(s->str + s->len + 2) = a3;
105
+ s->len += 3;
106
+
107
+ return s->len;
108
+ }
109
+
110
+ WString*
111
+ WStr_allocWithUTF8(WString* s, const char* in)
112
+ {
113
+ int i;
114
+ int u = 0;
115
+ int rest = 0;
116
+
117
+ WStr_alloc(s);
118
+ for (i = 0; in[i] != '\0'; i++) {
119
+ unsigned char c = in[i];
120
+ if (c >= 128 && c < 192) {
121
+ if (rest == 0)
122
+ return NULL;
123
+ u = (u << 6) | (c & 63);
124
+ rest--;
125
+ if (rest == 0) {
126
+ WStr_addWChar(s, u);
127
+ }
128
+ }
129
+ else if (c < 128) { /* 0b0nnnnnnn (7bit) */
130
+ if (c == 0)
131
+ return NULL;
132
+ WStr_addWChar(s, c);
133
+ rest = 0;
134
+ }
135
+ else if (c < 224) { /* 0b110nnnnn (11bit) */
136
+ rest = 1;
137
+ u = c & 31;
138
+ }
139
+ else if (c < 240) { /* 0b1110nnnn (16bit) */
140
+ rest = 2;
141
+ u = c & 15;
142
+ }
143
+ else if (c < 248) { /* 0b11110nnn (21bit) */
144
+ rest = 3;
145
+ u = c & 7;
146
+ }
147
+ else if (c < 252) { /* 0b111110nn (26bit) */
148
+ rest = 4;
149
+ u = c & 3;
150
+ }
151
+ else if (c < 254) { /* 0b1111110n (31bit) */
152
+ rest = 5;
153
+ u = c & 1;
154
+ }
155
+ else {
156
+ return NULL;
157
+ }
158
+ }
159
+
160
+ return s;
161
+ }
162
+
163
+ UString*
164
+ WStr_convertIntoUString(WString* wstr, UString* ustr)
165
+ {
166
+ int i;
167
+
168
+ for (i = 0; i < wstr->len; i++) {
169
+ UStr_addWChar(ustr, wstr->str[i]);
170
+ }
171
+
172
+ return ustr;
173
+ }
174
+
175
+ void
176
+ WStr_dump(WString* s)
177
+ {
178
+ int i;
179
+
180
+ printf("[%d/%d] ", s->len, s->size);
181
+ for (i = 0; i < s->len ; i++) {
182
+ printf("%04x ", *(s->str + i));
183
+ }
184
+ printf("\n");
185
+ }
data/ext/wstring.h ADDED
@@ -0,0 +1,41 @@
1
+ /*
2
+ * Simple wide string library
3
+ * Version 0.1
4
+ * 1999 by yoshidam
5
+ */
6
+
7
+ #ifndef _WSTRING_H
8
+ #define _WSTRING_H
9
+
10
+ #include "ustring.h"
11
+
12
+ #ifdef __cplusplus
13
+ extern "C" {
14
+ #endif
15
+
16
+ #define WSTR_INITIAL_STRING_LEN 1024
17
+ #define WSTR_STRING_EXTEND_LEN 1024
18
+
19
+ typedef struct _WString {
20
+ int* str;
21
+ int len;
22
+ int size;
23
+ } WString;
24
+
25
+ WString* WStr_alloc(WString* str);
26
+ WString* WStr_allocWithUTF8(WString* s, const char* u);
27
+ WString* WStr_enlarge(WString* str, int size);
28
+ void WStr_free(WString* str);
29
+ int WStr_addWChars(WString* s, const int* a, int len);
30
+ int WStr_addWChar(WString* s, int a);
31
+ int WStr_pushWString(WString* s, const WString* add);
32
+ int WStr_addWChar2(WString* s, int a1, int a2);
33
+ int WStr_addWChar3(WString* s, int a1, int a2, int a3);
34
+ UString* WStr_convertIntoUString(WString* wstr, UString* ustr);
35
+ void WStr_dump(WString* s);
36
+
37
+ #ifdef __cplusplus
38
+ }
39
+ #endif
40
+
41
+ #endif