1 /* -*- c-basic-offset:2; tab-width:2; indent-tabs-mode:nil -*- */
2 
3 #include <string.h>
4 
5 #include "vt_str.h"
6 
7 #include <pobl/bl_debug.h>
8 #include <pobl/bl_mem.h> /* malloc */
9 
10 /* --- global functions --- */
11 
12 /*
13  * string functions
14  */
15 
vt_str_init(vt_char_t * str,u_int size)16 void vt_str_init(vt_char_t *str, u_int size) {
17   u_int count;
18 
19   for (count = 0; count < size; count++) {
20     vt_char_init(str++);
21   }
22 }
23 
vt_str_new(u_int size)24 vt_char_t *vt_str_new(u_int size) {
25   vt_char_t *str;
26 
27   if ((str = malloc(sizeof(vt_char_t) * size)) == NULL) {
28 #ifdef DEBUG
29     bl_warn_printf(BL_DEBUG_TAG " malloc() failed.\n");
30 #endif
31 
32     return NULL;
33   }
34 
35   vt_str_init(str, size);
36 
37   return str;
38 }
39 
vt_str_final(vt_char_t * str,u_int size)40 void vt_str_final(vt_char_t *str, u_int size) {
41   u_int count;
42 
43   for (count = 0; count < size; count++) {
44     vt_char_final(&str[count]);
45   }
46 }
47 
vt_str_destroy(vt_char_t * str,u_int size)48 void vt_str_destroy(vt_char_t *str, u_int size) {
49   vt_str_final(str, size);
50   free(str);
51 }
52 
53 /*
54  * dst and src may overlap.
55  */
vt_str_copy(vt_char_t * dst,vt_char_t * src,u_int size)56 int vt_str_copy(vt_char_t *dst, vt_char_t *src, u_int size) {
57   u_int count;
58 
59   if (size == 0 || dst == src) {
60     return 0;
61   }
62 
63   if (dst < src) {
64     for (count = 0; count < size; count++) {
65       vt_char_copy(dst++, src++);
66     }
67   } else if (dst > src) {
68     dst += size;
69     src += size;
70     for (count = 0; count < size; count++) {
71       vt_char_copy(--dst, --src);
72     }
73   }
74 
75   return 1;
76 }
77 
vt_str_cols(vt_char_t * chars,u_int len)78 u_int vt_str_cols(vt_char_t *chars, u_int len) {
79   int count;
80   u_int cols;
81 
82   cols = 0;
83 
84   for (count = 0; count < len; count++) {
85     cols += vt_char_cols(&chars[count]);
86   }
87 
88   return cols;
89 }
90 
91 /* cols must be == vt_str_cols(chars) or <= vt_str_cols(chars) - cols of EOL char. */
vt_str_cols_to_len(vt_char_t * chars,u_int * cols)92 u_int vt_str_cols_to_len(vt_char_t *chars, u_int *cols) {
93   u_int c = *cols;
94   u_int len = 0;
95   u_int tmp;
96 
97   while (1) {
98     tmp = vt_char_cols(chars + (len++));
99     /* XXX In case tmp == 0... */
100     if (tmp >= c) {
101       if (tmp > c) {
102         len--;
103         *cols -= c;
104       }
105 
106       break;
107     } else {
108       c -= tmp;
109     }
110   }
111 
112   return len;
113 }
114 
115 /*
116  * XXX
117  * Returns inaccurate result in dealing with combined characters.
118  * Even if they have the same bytes, false is returned since
119  * vt_char_t:multi_ch-s never point the same address.)
120  */
vt_str_equal(vt_char_t * str1,vt_char_t * str2,u_int len)121 int vt_str_equal(vt_char_t *str1, vt_char_t *str2, u_int len) {
122   return memcmp(str1, str2, sizeof(vt_char_t) * len) == 0;
123 }
124 
vt_str_bytes_equal(vt_char_t * str1,vt_char_t * str2,u_int len)125 int vt_str_bytes_equal(vt_char_t *str1, vt_char_t *str2, u_int len) {
126   int count;
127 
128   for (count = 0; count < len; count++) {
129     if (!vt_char_code_equal(str1++, str2++)) {
130       return 0;
131     }
132   }
133 
134   return 1;
135 }
136 
137 #ifdef DEBUG
138 
vt_str_dump(vt_char_t * chars,u_int len)139 void vt_str_dump(vt_char_t *chars, u_int len) {
140   int count;
141 
142   for (count = 0; count < len; count++) {
143     vt_char_dump(&chars[count]);
144   }
145 
146   bl_msg_printf("\n");
147 }
148 
149 #endif
150