Remove some uses of sprintf to improve speed a tiny bit
This commit is contained in:
parent
fd09eb3b20
commit
95b5b2cb83
@ -22,6 +22,8 @@
|
||||
#include "rjp.h"
|
||||
|
||||
int irjp_is_whitespace(char c);
|
||||
int irjp_copy_string_quoted(char* restrict dest, const char* restrict string, RJP_index length);
|
||||
int irjp_copy_string_keyed(char* restrict dest, const char* restrict string, RJP_index length);
|
||||
char* irjp_convert_string(const char* str, RJP_index length, RJP_index* newlen);
|
||||
RJP_index irjp_array_strlen(const RJP_value* arr, const int flags, int depth);
|
||||
RJP_index irjp_object_strlen(const RJP_value* root, const int flags, int depth);
|
||||
|
||||
24
src/output.c
24
src/output.c
@ -162,13 +162,20 @@ static RJP_index irjp_write_value(char* dest, const RJP_value* val, const int fl
|
||||
ret = sprintf(dest, "%lf", val->dfloat);
|
||||
break;
|
||||
case rjp_json_boolean:
|
||||
ret = sprintf(dest, val->boolean ? "true" : "false");
|
||||
if(val->boolean){
|
||||
memcpy(dest, "true", 4);
|
||||
ret = 4;
|
||||
}else{
|
||||
memcpy(dest, "false", 5);
|
||||
ret = 5;
|
||||
}
|
||||
break;
|
||||
case rjp_json_null:
|
||||
ret = sprintf(dest, "null");
|
||||
memcpy(dest, "null", 4);
|
||||
ret = 4;
|
||||
break;
|
||||
case rjp_json_string:;
|
||||
ret = sprintf(dest, "\"%s\"", val->string.value);
|
||||
ret = irjp_copy_string_quoted(dest, val->string.value, val->string.length);
|
||||
break;
|
||||
case rjp_json_array:
|
||||
ret = irjp_dump_array(val, dest, flags, depth);
|
||||
@ -191,8 +198,10 @@ RJP_index irjp_dump_array(const RJP_value* arr, char* dest, const int flags, int
|
||||
RJP_value* current = rjp_array_iterator_current(&it);
|
||||
|
||||
if(!current){
|
||||
pos += sprintf(dest, "[]");
|
||||
rjp_delete_array_iterator(&it);
|
||||
dest[pos++] = '[';
|
||||
dest[pos++] = ']';
|
||||
dest[pos] = 0;
|
||||
return pos;
|
||||
}
|
||||
dest[pos++] = '[';
|
||||
@ -230,8 +239,10 @@ RJP_index irjp_dump_object(const RJP_value* root, char* dest, const int flags, i
|
||||
RJP_index pos = 0;
|
||||
RJP_value* current = rjp_object_iterator_current(&it);
|
||||
if(!current){
|
||||
pos += sprintf(dest, "{}");
|
||||
rjp_delete_object_iterator(&it);
|
||||
dest[pos++] = '{';
|
||||
dest[pos++] = '}';
|
||||
dest[pos] = 0;
|
||||
return pos;
|
||||
}
|
||||
dest[pos++] = '{';
|
||||
@ -239,10 +250,11 @@ RJP_index irjp_dump_object(const RJP_value* root, char* dest, const int flags, i
|
||||
dest[pos++] = '\n';
|
||||
|
||||
for(;current;current = rjp_object_iterator_next(&it)){
|
||||
const RJP_string* key = rjp_member_key(current);
|
||||
if(flags & RJP_FORMAT_TABBED_LINES)
|
||||
for(int i = 0;i < (depth+1);++i)
|
||||
dest[pos++] = '\t';
|
||||
pos += sprintf(dest+pos, "\"%s\":", rjp_member_key(current)->value);
|
||||
pos += irjp_copy_string_keyed(dest+pos, key->value, key->length);
|
||||
if(flags & RJP_FORMAT_KEY_SPACES)
|
||||
dest[pos++] = ' ';
|
||||
pos += irjp_write_value(dest+pos, current, flags, depth+1);
|
||||
|
||||
@ -152,6 +152,17 @@ static int codepoint_to_u8(char* dest, uint32_t codepoint){
|
||||
}
|
||||
}*/
|
||||
|
||||
int irjp_copy_string_quoted(char* restrict dest, const char* restrict string, RJP_index length){
|
||||
dest[0] = '"';
|
||||
memcpy(dest+1, string, length);
|
||||
dest[length+1] = '"';
|
||||
return length+2;
|
||||
}
|
||||
int irjp_copy_string_keyed(char* restrict dest, const char* restrict string, RJP_index length){
|
||||
int offset = irjp_copy_string_quoted(dest, string, length);
|
||||
dest[offset] = ':';
|
||||
return offset+1;
|
||||
}
|
||||
char* irjp_convert_string(const char* str, RJP_index length, RJP_index* newlen){
|
||||
char* newstring;
|
||||
RJP_index oldpos = 1; //ignore opening quote
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user