Commit | Line | Data |
---|---|---|
b0322a85 CE |
1 | /* |
2 | ** Copyright 2011 Double Precision, Inc. | |
3 | ** See COPYING for distribution information. | |
4 | ** | |
5 | */ | |
6 | ||
7 | #include "unicode_config.h" | |
8 | #include "unicode.h" | |
9 | #include <stdlib.h> | |
10 | #include <string.h> | |
11 | ||
12 | void unicode_buf_init(struct unicode_buf *p, size_t max) | |
13 | { | |
14 | p->ptr=0; | |
15 | p->size=0; | |
16 | p->len=0; | |
17 | p->max=max; | |
18 | } | |
19 | ||
20 | void unicode_buf_deinit(struct unicode_buf *p) | |
21 | { | |
22 | if (p->ptr) | |
23 | free(p->ptr); | |
24 | } | |
25 | ||
26 | int unicode_buf_append(struct unicode_buf *p, | |
27 | const unicode_char *uc, size_t l) | |
28 | { | |
29 | if (l > p->max-p->len) | |
30 | l=p->max-p->len; | |
31 | ||
32 | if (p->len + l > p->size) | |
33 | { | |
34 | size_t n=(p->len + l) * 2; | |
35 | unicode_char *newp; | |
36 | ||
37 | if (n < 256) | |
38 | n=256; | |
39 | ||
40 | if (n > p->max) | |
41 | n=p->max; | |
42 | ||
43 | newp=p->ptr ? realloc(p->ptr, n * sizeof(unicode_char)) | |
44 | : malloc(n * sizeof(unicode_char)); | |
45 | ||
46 | if (!newp) | |
47 | return -1; | |
48 | ||
49 | p->ptr=newp; | |
50 | p->size=n; | |
51 | } | |
52 | ||
53 | memcpy(p->ptr + p->len, uc, l * sizeof(unicode_char)); | |
54 | ||
55 | p->len += l; | |
56 | return 0; | |
57 | } | |
58 | ||
59 | void unicode_buf_append_char(struct unicode_buf *dst, | |
60 | const char *str, | |
61 | size_t cnt) | |
62 | { | |
63 | unicode_char unicode_buf[256]; | |
64 | ||
65 | while (cnt) | |
66 | { | |
67 | size_t n=sizeof(unicode_buf)/sizeof(unicode_buf[0]), i; | |
68 | ||
69 | if (n > cnt) | |
70 | n=cnt; | |
71 | ||
72 | for (i=0; i<n; ++i) | |
73 | unicode_buf[i]=(unsigned char)str[i]; | |
74 | ||
75 | str += n; | |
76 | cnt -= n; | |
77 | unicode_buf_append(dst, unicode_buf, i); | |
78 | } | |
79 | } | |
80 | ||
81 | void unicode_buf_remove(struct unicode_buf *p, | |
82 | size_t pos, | |
83 | size_t cnt) | |
84 | { | |
85 | if (pos > p->len) | |
86 | pos=p->len; | |
87 | ||
88 | if (cnt > p->len-pos) | |
89 | cnt=p->len-pos; | |
90 | ||
91 | if (cnt) | |
92 | memmove(p->ptr+pos+cnt, p->ptr+pos, p->len-pos-cnt); | |
93 | p->len -= cnt; | |
94 | } | |
95 | ||
96 | int unicode_buf_cmp(const struct unicode_buf *a, | |
97 | const struct unicode_buf *b) | |
98 | { | |
99 | size_t i; | |
100 | ||
101 | for (i=0; i<a->len && i<b->len; i++) | |
102 | { | |
103 | if (a->ptr[i] < b->ptr[i]) | |
104 | return -1; | |
105 | if (a->ptr[i] > b->ptr[i]) | |
106 | return 1; | |
107 | } | |
108 | ||
109 | return (a->len < b->len ? -1:a->len > b->len ? 1:0); | |
110 | } | |
111 | ||
112 | int unicode_buf_cmp_str(const struct unicode_buf *p, const char *c, | |
113 | size_t cl) | |
114 | { | |
115 | size_t i; | |
116 | ||
117 | for (i=0; i<p->len && i < cl; ++i) | |
118 | { | |
119 | if (p->ptr[i] < c[i]) | |
120 | return -1; | |
121 | ||
122 | if (p->ptr[i] > c[i]) | |
123 | return 1; | |
124 | } | |
125 | ||
126 | return (p->len < cl ? -1: p->len > cl ? 1:0); | |
127 | } |