Skip to content

Commit dc118f9

Browse files
committed
Refactoring replace functions of basic_string_t.
1 parent bb9bb13 commit dc118f9

File tree

3 files changed

+147
-533
lines changed

3 files changed

+147
-533
lines changed

src/cstl_basic_string.c

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,15 @@ size_t basic_string_copy(const basic_string_t* cpt_basic_string, void* pv_buffer
203203
pby_pos = cpt_basic_string->_pby_string + t_pos * t_typesize;
204204
pby_terminator = cpt_basic_string->_pby_string + basic_string_size(cpt_basic_string) * t_typesize;
205205

206-
/*
207-
* It does not require judgment terminator,
208-
* when the type style is c built-in type,
209-
* which improves efficiency.
210-
*/
211-
if (_GET_BASIC_STRING_TYPE_STYLE(cpt_basic_string) == _TYPE_C_BUILTIN) {
206+
if (strncmp(_GET_BASIC_STRING_TYPE_BASENAME(cpt_basic_string), _C_STRING_TYPE, _TYPE_NAME_SIZE) == 0) {
207+
for (i = 0; i < t_size; ++i) {
208+
if (memcmp(pby_terminator, pby_pos + i * t_typesize, t_typesize) != 0) {
209+
*((const char**)pv_buffer + i) = string_c_str((string_t*)(pby_pos + i * t_typesize));
210+
} else {
211+
*((const char**)pv_buffer + i) = NULL;
212+
}
213+
}
214+
} else if (_GET_BASIC_STRING_TYPE_STYLE(cpt_basic_string) == _TYPE_C_BUILTIN) {
212215
for (i = 0; i < t_size; ++i) {
213216
b_result = _GET_BASIC_STRING_TYPE_SIZE(cpt_basic_string);
214217
_GET_BASIC_STRING_TYPE_COPY_FUNCTION(cpt_basic_string)(
@@ -217,8 +220,11 @@ size_t basic_string_copy(const basic_string_t* cpt_basic_string, void* pv_buffer
217220
}
218221
} else {
219222
for (i = 0; i < t_size; ++i) {
220-
_basic_string_copy_elem_with_terminator(
221-
cpt_basic_string, (_byte_t*)pv_buffer + i * t_typesize, pby_pos + i * t_typesize, pby_terminator);
223+
if (memcmp(pby_terminator, pby_pos + i * t_typesize, t_typesize) != 0) {
224+
*((const _byte_t**)pv_buffer + i) = pby_pos + i * t_typesize;
225+
} else {
226+
*((const _byte_t**)pv_buffer + i) = NULL;
227+
}
222228
}
223229
}
224230

@@ -445,7 +451,8 @@ int basic_string_compare_substring_substring(
445451
pby_first = cpt_first->_pby_string + t_firstpos * t_typesize;
446452
pby_second = cpt_second->_pby_string + t_secondpos * t_typesize;
447453
pby_terminator = cpt_first->_pby_string + basic_string_size(cpt_first) * t_typesize;
448-
if (_GET_BASIC_STRING_TYPE_STYLE(cpt_first) == _TYPE_CSTL_BUILTIN) {
454+
if (_GET_BASIC_STRING_TYPE_STYLE(cpt_first) == _TYPE_CSTL_BUILTIN ||
455+
strncmp(_GET_BASIC_STRING_TYPE_BASENAME(cpt_first), _C_STRING_TYPE, _TYPE_NAME_SIZE) == 0) {
449456
for (i = 0; i < t_cmplen; ++i) {
450457
int n_first = memcmp(pby_terminator, pby_first, t_typesize);
451458
int n_second = memcmp(pby_terminator, pby_second, t_typesize);
@@ -554,9 +561,18 @@ int basic_string_compare_substring_subcstr(
554561
/* char* */
555562
if (strncmp(_GET_BASIC_STRING_TYPE_BASENAME(cpt_basic_string), _C_STRING_TYPE, _TYPE_NAME_SIZE) == 0) {
556563
for (i = 0; i < t_cmplen; ++i) {
557-
int n_result = string_compare_cstr((string_t*)(pby_string + i * t_typesize), *((char**)cpv_value_string + i));
558-
if (n_result != 0) {
559-
return n_result;
564+
int n_result = memcmp(pby_terminator, pby_string + i * t_typesize, t_typesize);
565+
566+
if (n_result == 0 && *((char**)cpv_value_string + i) != NULL) {
567+
return -1;
568+
} else if (n_result != 0 && *((char**)cpv_value_string + i) == NULL) {
569+
return 1;
570+
} else if (n_result != 0 && *((char**)cpv_value_string + i) != NULL) {
571+
int n_cmp_result = string_compare_cstr(
572+
(string_t*)(pby_string + i * t_typesize), *((char**)cpv_value_string + i));
573+
if (n_cmp_result != 0) {
574+
return n_cmp_result;
575+
}
560576
}
561577
}
562578
} else if (_GET_BASIC_STRING_TYPE_STYLE(cpt_basic_string) == _TYPE_C_BUILTIN) {

src/cstl_basic_string_aux.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,8 @@ void _basic_string_destroy_elem_range_auxiliary(
579579
pby_terminator = cpt_basic_string->_pby_string +
580580
basic_string_size(cpt_basic_string) * _GET_BASIC_STRING_TYPE_SIZE(cpt_basic_string);
581581

582-
if (_GET_BASIC_STRING_TYPE_STYLE(cpt_basic_string) == _TYPE_C_BUILTIN) {
582+
if (_GET_BASIC_STRING_TYPE_STYLE(cpt_basic_string) == _TYPE_C_BUILTIN &&
583+
strncmp(_GET_BASIC_STRING_TYPE_BASENAME(cpt_basic_string), _C_STRING_TYPE, _TYPE_NAME_SIZE) != 0) {
583584
for (i = 0; i < t_len; ++i) {
584585
bool_t b_result = _GET_BASIC_STRING_TYPE_SIZE(cpt_basic_string);
585586
_GET_BASIC_STRING_TYPE_DESTROY_FUNCTION(cpt_basic_string)(pby_del, &b_result);

0 commit comments

Comments
 (0)