Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanups #32

Merged
merged 3 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion PackageInfo.g
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Persons := [
IsMaintainer := true,
FirstNames := "Christopher",
LastName := "Jefferson",
WWWHome := "http://caj.host.cs.st-andrews.ac.uk/",
WWWHome := "https://heather.cafe/",
Email := "[email protected]",
PostalAddress := Concatenation(
"St Andrews\n",
Expand Down
27 changes: 6 additions & 21 deletions src/json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ static Obj FuncJSON_ESCAPE_STRING(Obj self, Obj param)
return param;

// Massively over-long string
Obj copy = NEW_STRING(lenString * 6);
Obj copy = NEW_STRING(lenString * 6 + 7);
UChar * base = CHARS_STRING(copy);
UChar * out = base;
Int i = 1;
Expand All @@ -211,7 +211,7 @@ static Obj FuncJSON_ESCAPE_STRING(Obj self, Obj param)
default:
if(u < ' ')
{
sprintf((char*)out,"\\u%04X",(unsigned)u);
snprintf((char*)out,7, "\\u%04X",(unsigned)u);
out += 6;
}
else
Expand All @@ -226,37 +226,22 @@ static Obj FuncJSON_ESCAPE_STRING(Obj self, Obj param)
return copy;
}

// Local copy of AppendCStr, as it is a new method in 4.12
void JSON_AppendCStr(Obj str, const char * buf, UInt len)
{
GAP_ASSERT(IS_MUTABLE_OBJ(str));
GAP_ASSERT(IS_STRING_REP(str));

UInt len1 = GET_LEN_STRING(str);
UInt newlen = len1 + len;
GROW_STRING(str, newlen);
SET_LEN_STRING(str, newlen);
CLEAR_FILTS_LIST(str);
memcpy(CHARS_STRING(str) + len1, buf, len);
CHARS_STRING(str)[newlen] = '\0'; // add terminator
}

static Obj FuncGAP_LIST_TO_JSON_STRING(Obj self, Obj string, Obj stream, Obj list) {
RequireDenseList("list", list);
Int len = LEN_LIST(list);
char buf[50] = {};

// Call this at the start
ConvString(string);
JSON_AppendCStr(string, "[", 1);
AppendCStr(string, "[", 1);
for(int i = 1; i <= len; ++i) {
if(i != 1) {
JSON_AppendCStr(string, ",", 1);
AppendCStr(string, ",", 1);
}
Obj val = ELM_LIST(list, i);
if(IS_INTOBJ(val)) {
snprintf(buf, sizeof(buf), "%ld", INT_INTOBJ(val));
JSON_AppendCStr(string, buf, strlen(buf));
AppendCStr(string, buf, strlen(buf));
} else if(IS_LIST(val) && !(IS_STRING(val))) {
FuncGAP_LIST_TO_JSON_STRING(self, string, stream, val);
} else {
Expand All @@ -265,7 +250,7 @@ static Obj FuncGAP_LIST_TO_JSON_STRING(Obj self, Obj string, Obj stream, Obj lis
ConvString(string);
}
}
JSON_AppendCStr(string, "]", 1);
AppendCStr(string, "]", 1);

return 0;
}
Expand Down
Loading