• R/O
  • SSH
  • HTTPS

cstl: Commit


Commit MetaInfo

Revision36 (tree)
Time2007-03-22 22:44:36
Authorkatono

Log Message

insert()とreplace()を修正。

Change Summary

Incremental Difference

--- branches/try-change-assign/test/string_test.cpp (revision 35)
+++ branches/try-change-assign/test/string_test.cpp (revision 36)
@@ -146,6 +146,9 @@
146146 String_assign(x, "abcdefghijklmnopqrstuvwxyz", NPOS);
147147 assert(String_size(x) == 26);
148148 assert(strcmp("abcdefghijklmnopqrstuvwxyz", String_c_str(x)) == 0);
149+ String_assign(x, String_c_str(x), NPOS);
150+ assert(String_size(x) == 26);
151+ assert(strcmp("abcdefghijklmnopqrstuvwxyz", String_c_str(x)) == 0);
149152 String_assign(x, "abcdefghijklmnopqrstuvwxyz", 10);
150153 assert(String_size(x) == 10);
151154 assert(strcmp("abcdefghij", String_c_str(x)) == 0);
@@ -184,6 +187,9 @@
184187 String_append(x, "m", NPOS);
185188 assert(String_size(x) == 13);
186189 assert(strcmp("abcdefghijklm", String_c_str(x)) == 0);
190+ String_append(x, String_c_str(x) + 1, NPOS);
191+ assert(String_size(x) == 25);
192+ assert(strcmp("abcdefghijklmbcdefghijklm", String_c_str(x)) == 0);
187193 String_clear(x);
188194 assert(String_size(x) == 0);
189195 /* append_c */
@@ -226,6 +232,10 @@
226232 String_insert(x, 5, "rst", 0);
227233 assert(String_size(x) == 17);
228234 assert(strcmp("gijkhabcdeflmopqn", String_c_str(x)) == 0);
235+ String_insert(x, 1, String_c_str(x), NPOS);
236+ assert(String_size(x) == 34);
237+ printf("%s\n", String_c_str(x));
238+ assert(strcmp("ggijkhabcdeflmopqnijkhabcdeflmopqn", String_c_str(x)) == 0);
229239 String_clear(x);
230240 assert(String_size(x) == 0);
231241 /* insert_c */
@@ -298,6 +308,10 @@
298308 String_replace(x, 0, 3, "hogehoge", 4);
299309 assert(String_size(x) == 27);
300310 assert(strcmp("hogeDEFGHIjklmnopqrstuvwXYZ", String_c_str(x)) == 0);
311+ String_replace(x, 3, 4, String_c_str(x), 4);
312+ assert(String_size(x) == 27);
313+ printf("%s\n", String_c_str(x));
314+ assert(strcmp("hoghogeGHIjklmnopqrstuvwXYZ", String_c_str(x)) == 0);
301315 String_replace(x, 0, NPOS, "", NPOS);
302316 assert(String_size(x) == 0);
303317 assert(strcmp("", String_c_str(x)) == 0);
--- branches/try-change-assign/cstl/string.h (revision 35)
+++ branches/try-change-assign/cstl/string.h (revision 36)
@@ -359,6 +359,7 @@
359359 \
360360 int Name##_insert(Name *self, size_t idx, Type *cstr, size_t cstr_len)\
361361 {\
362+ int ret;\
362363 assert(self && "String_insert");\
363364 assert(self->magic == self && "String_insert");\
364365 assert(Name##_size(self) >= idx && "String_insert");\
@@ -366,7 +367,20 @@
366367 if (cstr_len == NPOS) {\
367368 cstr_len = Name##my_strlen(cstr);\
368369 }\
369- return Name##CharVector_insert_n(self->data, idx, cstr, cstr_len);\
370+ if (cstr_len && Name##_c_str(self) + idx < cstr + cstr_len && cstr < Name##_c_str(self) + Name##_size(self)) {\
371+ size_t i;\
372+ Type *buf;\
373+ buf = (Type *) malloc(sizeof(Type) * cstr_len);\
374+ if (!buf) return 0;\
375+ for (i = 0; i < cstr_len; i++) {\
376+ buf[i] = cstr[i];\
377+ }\
378+ ret = Name##CharVector_insert_n(self->data, idx, buf, cstr_len);\
379+ free(buf);\
380+ } else {\
381+ ret = Name##CharVector_insert_n(self->data, idx, cstr, cstr_len);\
382+ }\
383+ return ret;\
370384 }\
371385 \
372386 int Name##_insert_c(Name *self, size_t idx, size_t n, Type c)\
@@ -389,6 +403,8 @@
389403 {\
390404 size_t i;\
391405 size_t size;\
406+ Type *buf;\
407+ int flag = 0;\
392408 assert(self && "String_replace");\
393409 assert(self->magic == self && "String_replace");\
394410 assert(cstr && "String_replace");\
@@ -400,11 +416,21 @@
400416 if (cstr_len == NPOS) {\
401417 cstr_len = Name##my_strlen(cstr);\
402418 }\
419+ if (cstr_len && Name##_c_str(self) + idx < cstr + cstr_len && cstr < Name##_c_str(self) + Name##_size(self)) {\
420+ buf = (Type *) malloc(sizeof(Type) * cstr_len);\
421+ if (!buf) return 0;\
422+ for (i = 0; i < cstr_len; i++) {\
423+ buf[i] = cstr[i];\
424+ }\
425+ flag = 1;\
426+ } else {\
427+ buf = cstr;\
428+ }\
403429 if (cstr_len <= len) {\
404430 /* 拡張必要なし */\
405431 for (i = 0; i < len; i++) {\
406432 if (i < cstr_len) {\
407- *Name##_at(self, i + idx) = cstr[i];\
433+ *Name##_at(self, i + idx) = buf[i];\
408434 } else {\
409435 size_t j = len - cstr_len;\
410436 if (j > Name##_size(self) - (cstr_len + idx)) {\
@@ -417,13 +443,15 @@
417443 } else {\
418444 /* 拡張必要あり */\
419445 if (!Name##_reserve(self, Name##_capacity(self) + (cstr_len - len))) {\
446+ if (flag) free(buf);\
420447 return 0;\
421448 }\
422449 for (i = 0; i < len; i++) {\
423- *Name##_at(self, i + idx) = cstr[i];\
450+ *Name##_at(self, i + idx) = buf[i];\
424451 }\
425- Name##_insert(self, len + idx, &cstr[len], cstr_len - len);\
452+ Name##_insert(self, len + idx, &buf[len], cstr_len - len);\
426453 }\
454+ if (flag) free(buf);\
427455 return 1;\
428456 }\
429457 \
Show on old repository browser