Skip to content

Commit

Permalink
add: count和length独立开(step1)
Browse files Browse the repository at this point in the history
  • Loading branch information
XiangYyang committed Jun 21, 2023
1 parent a72a1b3 commit 23eadb7
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 27 deletions.
8 changes: 8 additions & 0 deletions inc/mm_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@
#define mstr_unreachable() ((void)0U)
#endif // _MSTR_RUNTIME_CTRLFLOW_MARKER

#if !defined(_MSTR_USE_UTF_8)
/**
* @brief 指定是否启用UTF-8支持
*
*/
#define _MSTR_USE_UTF_8 1
#endif // _MSTR_USE_UTF_8

//
// 导出函数修辞
//
Expand Down
8 changes: 7 additions & 1 deletion inc/mm_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,20 @@ typedef struct tagMString
*/
char stack_region[MSTR_STACK_REGION_SIZE];

/**
* @brief 字符串的字节长度
*
*/
usize_t count;

/**
* @brief 字符串长度
*
*/
usize_t length;

/**
* @brief 已经分配了的内存大小, cap_size >= length + 1
* @brief 已经分配了的内存大小, cap_size >= count + 1
*
*/
usize_t cap_size;
Expand Down
2 changes: 1 addition & 1 deletion inc/mm_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ class string final
*/
std::string as_std_string()
{
return std::string(this_obj.buff, this_obj.length);
return std::string(this_obj.buff, this_obj.count);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions inc/mm_typedef.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ enum
False = false,
};

/**
* @brief unicode代码点
*
*/
typedef uint32_t mstr_codepoint_t;

/**
* @brief RTC时间
*
Expand Down
2 changes: 1 addition & 1 deletion src/mm_fmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ static mstr_result_t copy_to_output(
return mstr_concat(out_str, src_str);
}
// 计算宽度够不够
src_len = src_str->length;
src_len = src_str->count;
need_width = (usize_t)fmt_spec->width;
if (src_len >= need_width) {
// 宽度太宽, 不管对齐了
Expand Down
2 changes: 1 addition & 1 deletion src/mm_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ mstr_iovformat(
// 写到输出
MSTR_AND_THEN(
res,
io->io_write(io->capture, (const byte_t*)buff.buff, buff.length)
io->io_write(io->capture, (const byte_t*)buff.buff, buff.count)
);
// 释放
if (MSTR_SUCC(res_create)) {
Expand Down
47 changes: 24 additions & 23 deletions src/mm_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ mstr_create(MString* str, const char* content)
(usize_t)strlen(content);
if (content_len == 0) {
str->buff = str->stack_region;
str->count = 0;
str->length = 0;
str->cap_size = MSTR_STACK_REGION_SIZE;
return MStr_Ok;
}
else if (content_len < MSTR_STACK_REGION_SIZE) {
str->buff = str->stack_region;
str->length = content_len;
str->count = content_len;
str->cap_size = MSTR_STACK_REGION_SIZE;
strcpy(str->buff, content);
return MStr_Ok;
Expand All @@ -60,7 +61,7 @@ mstr_create(MString* str, const char* content)
// 内存分配失败
return MStr_Err_HeapTooSmall;
}
str->length = content_len;
str->count = content_len;
strcpy(str->buff, content);
return MStr_Ok;
}
Expand All @@ -71,18 +72,18 @@ mstr_move_create(MString* str, MString* other)
{
if (other->buff == other->stack_region) {
str->buff = str->stack_region;
str->length = other->length;
str->count = other->count;
str->cap_size = MSTR_STACK_REGION_SIZE;
// 复制stack上的内容
memcpy(str->buff, other->buff, other->length);
memcpy(str->buff, other->buff, other->count);
}
else {
str->buff = other->buff;
str->length = other->length;
str->count = other->count;
str->cap_size = other->cap_size;
}
other->buff = NULL;
other->length = 0;
other->count = 0;
other->cap_size = 0;
}

Expand All @@ -97,7 +98,7 @@ mstr_copy_create(MString* str, const MString* other)

MSTR_EXPORT_API(void) mstr_clear(MString* str)
{
str->length = 0;
str->count = 0;
}

MSTR_EXPORT_API(mstr_result_t) mstr_append(MString* str, char ch)
Expand All @@ -113,7 +114,7 @@ mstr_repeat_append(MString* str, char ch, usize_t cnt)
}
else {
mstr_result_t result = MStr_Ok;
if (str->length + cnt + 1 >= str->cap_size) {
if (str->count + cnt + 1 >= str->cap_size) {
// 保证length < cap_size + 1
// 且有足够的空间存放下一个字符
MSTR_AND_THEN(
Expand All @@ -125,9 +126,9 @@ mstr_repeat_append(MString* str, char ch, usize_t cnt)
}
if (MSTR_SUCC(result)) {
for (usize_t i = 0; i < cnt; i += 1) {
str->buff[str->length + i] = ch;
str->buff[str->count + i] = ch;
}
str->length += cnt;
str->count += cnt;
}
return result;
}
Expand All @@ -137,22 +138,22 @@ MSTR_EXPORT_API(mstr_result_t)
mstr_concat(MString* str, const MString* other)
{
mstr_result_t result = MStr_Ok;
if (str->length + other->length >= str->cap_size) {
if (str->count + other->count >= str->cap_size) {
// 且有足够的空间存放
MSTR_AND_THEN(
result,
mstr_expand_size(
str, str->cap_size + other->length + MSTR_CAP_SIZE_STEP
str, str->cap_size + other->count + MSTR_CAP_SIZE_STEP
)
);
}
if (MSTR_SUCC(result)) {
char* dst = str->buff + str->length;
usize_t len = other->length;
char* dst = str->buff + str->count;
usize_t len = other->count;
const char* src = other->buff;
// 复制内容
memcpy(dst, src, len);
str->length += len;
str->count += len;
}
return result;
}
Expand All @@ -163,7 +164,7 @@ mstr_concat_cstr(MString* str, const char* other)
MString lit;
// const MString不会被修改, 所以可强转一下
lit.buff = (char*)(iptr_t)other;
lit.length = (usize_t)strlen(other);
lit.count = (usize_t)strlen(other);
lit.cap_size = 0;
return mstr_concat(str, &lit);
}
Expand All @@ -174,14 +175,14 @@ mstr_concat_cstr_slice(MString* str, const char* start, const char* end)
MString lit;
// const MString不会被修改, 所以可强转一下
lit.buff = (char*)(iptr_t)start;
lit.length = (usize_t)(end - start);
lit.count = (usize_t)(end - start);
lit.cap_size = 0;
return mstr_concat(str, &lit);
}

MSTR_EXPORT_API(mstr_result_t) mstr_reverse_self(MString* str)
{
char* pend = str->buff + str->length - 1;
char* pend = str->buff + str->count - 1;
char* pbeg = str->buff;
while (pbeg < pend) {
char v = *pend;
Expand All @@ -198,18 +199,18 @@ MSTR_EXPORT_API(const char*) mstr_as_cstr(MString* str)
{
// 在length的地方补上0
// 因为cap_size至少比length大1, 因此不需要担心内存问题
str->buff[str->length] = 0;
str->buff[str->count] = 0;
return str->buff;
}

MSTR_EXPORT_API(bool_t) mstr_equal(const MString* a, const MString* b)
{
if (a->length != b->length) {
if (a->count != b->count) {
return False;
}
else {
uint32_t bit = 0;
usize_t len = a->length;
usize_t len = a->count;
for (usize_t i = 0; i < len; i += 1) {
uint32_t ch_a = a->buff[i];
uint32_t ch_b = b->buff[i];
Expand All @@ -227,7 +228,7 @@ MSTR_EXPORT_API(void) mstr_free(MString* str)
}
// else: stack上分配的, 不用管它
str->buff = NULL;
str->length = 0;
str->count = 0;
str->cap_size = 0;
}

Expand All @@ -247,7 +248,7 @@ static mstr_result_t mstr_expand_size(MString* str, usize_t new_size)
char* new_ptr = (char*)mstr_realloc(
str->buff,
str->buff == str->stack_region,
str->length,
str->count,
new_size
);
if (new_ptr == NULL) {
Expand Down

0 comments on commit 23eadb7

Please sign in to comment.