5 Star 27 Fork 30

RichardGong/Craft A Language

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
string.c 1.42 KB
一键复制 编辑 原始数据 按行查看 历史
RichardGong 提交于 3年前 . 重新上传代码
#include "string.h"
#include "mem.h"
PlayString* string_create_by_length(size_t length){
//申请内存
size_t size = sizeof(Object) + sizeof(size_t) + sizeof(unsigned char)*(length+1);
PlayString * pstr = (PlayString*)PlayAlloc(size);
//设置字符串长度
pstr->length = length;
// //设置数据指针
// pstr->data = (char*)(pstr + sizeof(Object) + sizeof(size_t));
return pstr;
}
PlayString* string_create_by_cstr(const char* str){
size_t str_length = strlen(str);
PlayString * pstr = string_create_by_length(str_length);
//拷贝数据
strcpy(PTR_CSTRING(pstr), str);
return pstr;
}
void string_destroy(PlayString* str){
PlayFree((Object*)str);
}
int string_length(PlayString * str){
return str->length;
}
PlayString* string_concat(PlayString* str1, PlayString* str2){
size_t str_length1 = strlen(PTR_CSTRING(str1));
size_t str_length2 = strlen(PTR_CSTRING(str2));
size_t size = sizeof(Object) + sizeof(size_t) + sizeof(unsigned char)*(str_length1+str_length2+1);
//申请内存
PlayString * pstr = (PlayString*)PlayAlloc(size);
//设置字符串长度
pstr->length = str_length1 + str_length2;
//设置数据指针
// pstr->data = (char*)(pstr + sizeof(Object) + sizeof(size_t));
//拷贝数据
strcpy(PTR_CSTRING(pstr), PTR_CSTRING(str1));
strcpy(PTR_CSTRING(pstr)+str_length1, PTR_CSTRING(str2));
return pstr;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
TypeScript
1
https://gitee.com/richard-gong/craft-a-language.git
git@gitee.com:richard-gong/craft-a-language.git
richard-gong
craft-a-language
Craft A Language
master

搜索帮助