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

Update "format" so that control characters are escaped in a JSON-friendly way #3677

Open
wants to merge 6 commits into
base: development
Choose a base branch
from

Conversation

d-torrance
Copy link
Member

This is a quick follow-up to #3670. Now format plays nicely with JSON for control characters:

i1 : format ascii(0..31)

o1 = "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\
     u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f"

Closes: #3673

In particular, escape them in a JSON-friendly way, i.e., use the
shorter forms for \b, \f, \n, \r, and \t only.  All other others are
escaped as \u00xy.

Also add some new char arithmetic functions to make the code a bit cleaner.
Regular old "present" now does a better job
@d-torrance d-torrance requested a review from mahrud February 24, 2025 21:36
else if c == '\f' then (provide '\\'; provide 'f')
else if c == '\n' then (provide '\\'; provide 'n')
else if c == '\r' then (provide '\\'; provide 'r')
else if c == '\t' then (provide '\\'; provide 't')
else if c == '\v' then (provide '\\'; provide 'v')
else if 0 <= c && c < 32
then ( -- escape all other control characters as \u00xy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably fine, but you should probably add some tests or otherwise document the behavior somewhere.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants