support argNewStrN() api

This commit is contained in:
lyon 2023-06-17 15:45:35 +08:00
parent 3dc892904f
commit 21c6ed27aa
2 changed files with 24 additions and 15 deletions

View File

@ -280,7 +280,7 @@ Arg* arg_toStrArg(Arg* arg) {
if (type == ARG_TYPE_INT) { if (type == ARG_TYPE_INT) {
#if PIKA_PRINT_LLD_ENABLE #if PIKA_PRINT_LLD_ENABLE
pika_snprintf(buff, PIKA_SPRINTF_BUFF_SIZE, "%lld", pika_snprintf(buff, PIKA_SPRINTF_BUFF_SIZE, "%lld",
(long long int)arg_getInt(arg)); (long long int)arg_getInt(arg));
#else #else
pika_platform_snprintf(buff, PIKA_SPRINTF_BUFF_SIZE, "%d", pika_platform_snprintf(buff, PIKA_SPRINTF_BUFF_SIZE, "%d",
(int)arg_getInt(arg)); (int)arg_getInt(arg));
@ -297,8 +297,7 @@ Arg* arg_toStrArg(Arg* arg) {
goto __exit; goto __exit;
} }
if (type == ARG_TYPE_FLOAT) { if (type == ARG_TYPE_FLOAT) {
pika_snprintf(buff, PIKA_SPRINTF_BUFF_SIZE, "%f", pika_snprintf(buff, PIKA_SPRINTF_BUFF_SIZE, "%f", arg_getFloat(arg));
arg_getFloat(arg));
result = arg_newStr(buff); result = arg_newStr(buff);
goto __exit; goto __exit;
} }
@ -307,8 +306,7 @@ Arg* arg_toStrArg(Arg* arg) {
goto __exit; goto __exit;
} }
if (type == ARG_TYPE_POINTER) { if (type == ARG_TYPE_POINTER) {
pika_snprintf(buff, PIKA_SPRINTF_BUFF_SIZE, "%p", pika_snprintf(buff, PIKA_SPRINTF_BUFF_SIZE, "%p", arg_getPtr(arg));
arg_getPtr(arg));
result = arg_newStr(buff); result = arg_newStr(buff);
goto __exit; goto __exit;
} }
@ -324,25 +322,22 @@ Arg* arg_toStrArg(Arg* arg) {
strEqu(method_store->name, "list") || strEqu(method_store->name, "list") ||
strEqu(method_store->name, "dict") || strEqu(method_store->name, "dict") ||
strEqu(method_store->name, "tuple")) { strEqu(method_store->name, "tuple")) {
pika_snprintf(buff, PIKA_SPRINTF_BUFF_SIZE, pika_snprintf(buff, PIKA_SPRINTF_BUFF_SIZE, "<class '%s'>",
"<class '%s'>", method_store->name); method_store->name);
result = arg_newStr(buff); result = arg_newStr(buff);
goto __exit; goto __exit;
} }
pika_snprintf(buff, PIKA_SPRINTF_BUFF_SIZE, pika_snprintf(buff, PIKA_SPRINTF_BUFF_SIZE,
"<built-in function %s>", "<built-in function %s>", method_store->name);
method_store->name);
result = arg_newStr(buff); result = arg_newStr(buff);
goto __exit; goto __exit;
} }
if (argType_isConstructor(type)) { if (argType_isConstructor(type)) {
pika_snprintf(buff, PIKA_SPRINTF_BUFF_SIZE, pika_snprintf(buff, PIKA_SPRINTF_BUFF_SIZE, "<class 'object'>");
"<class 'object'>");
result = arg_newStr(buff); result = arg_newStr(buff);
goto __exit; goto __exit;
} }
pika_snprintf(buff, PIKA_SPRINTF_BUFF_SIZE, pika_snprintf(buff, PIKA_SPRINTF_BUFF_SIZE, "<class 'function'>");
"<class 'function'>");
result = arg_newStr(buff); result = arg_newStr(buff);
goto __exit; goto __exit;
} }
@ -355,8 +350,8 @@ Arg* arg_toStrArg(Arg* arg) {
goto __exit; goto __exit;
} }
if (type == ARG_TYPE_OBJECT_META) { if (type == ARG_TYPE_OBJECT_META) {
pika_snprintf(buff, PIKA_SPRINTF_BUFF_SIZE, pika_snprintf(buff, PIKA_SPRINTF_BUFF_SIZE, "<meta object at %p>",
"<meta object at %p>", arg_getPtr(arg)); arg_getPtr(arg));
result = arg_newStr(buff); result = arg_newStr(buff);
goto __exit; goto __exit;
} }
@ -452,6 +447,15 @@ Arg* arg_setPtr(Arg* self, char* name, ArgType type, void* pointer) {
return arg_set(self, name, type, (uint8_t*)&pointer, sizeof(uintptr_t)); return arg_set(self, name, type, (uint8_t*)&pointer, sizeof(uintptr_t));
} }
Arg* arg_setStrN(Arg* self, char* name, char* string, size_t len) {
if (NULL == string) {
return NULL;
}
Arg* ret = arg_set(self, name, ARG_TYPE_STRING, NULL, len + 1);
pika_platform_memcpy(arg_getContent(ret), string, len);
return ret;
}
Arg* arg_setStr(Arg* self, char* name, char* string) { Arg* arg_setStr(Arg* self, char* name, char* string) {
if (NULL == string) { if (NULL == string) {
return NULL; return NULL;

View File

@ -124,6 +124,7 @@ Arg* arg_setBool(Arg* self, char* name, pika_bool val);
Arg* arg_setFloat(Arg* self, char* name, pika_float val); Arg* arg_setFloat(Arg* self, char* name, pika_float val);
Arg* arg_setPtr(Arg* self, char* name, ArgType type, void* pointer); Arg* arg_setPtr(Arg* self, char* name, ArgType type, void* pointer);
Arg* arg_setStr(Arg* self, char* name, char* string); Arg* arg_setStr(Arg* self, char* name, char* string);
Arg* arg_setStrN(Arg* self, char* name, char* string, size_t len);
Arg* arg_setNone(Arg* self); Arg* arg_setNone(Arg* self);
Arg* arg_setBytes(Arg* self, char* name, uint8_t* src, size_t size); Arg* arg_setBytes(Arg* self, char* name, uint8_t* src, size_t size);
@ -147,6 +148,10 @@ static inline Arg* arg_newStr(char* string) {
return arg_setStr(NULL, (char*)"", (string)); return arg_setStr(NULL, (char*)"", (string));
} }
static inline Arg* arg_newStrN(char* string, size_t size) {
return arg_setStrN(NULL, (char*)"", (string), size);
}
static inline Arg* arg_newNone() { static inline Arg* arg_newNone() {
return arg_setNone(NULL); return arg_setNone(NULL);
} }