Giter VIP home page Giter VIP logo

Comments (5)

DanielSherlock avatar DanielSherlock commented on August 11, 2024

Having been pointed at gleam.mjs I can see it uses JSON.stringify.

Quick interactive testing on the MDN page for JSON.stringify makes clear that all five special characters are properly escaped. I don't know if Node does anything differently, but assuming not mean this is incompatible behaviour between the two targets.

from stdlib.

lpil avatar lpil commented on August 11, 2024

Thank you. Let's get this fixed

from stdlib.

DanielSherlock avatar DanielSherlock commented on August 11, 2024

I'm not able to do this properly right now, but I was curious so I made a little test project that had only the relevant bits of gleam_stdlib.erl and fumbled with the Erlang docs for a bit. Replacing as follows seemed to do the trick:

% At line 349: %
% --- %
% Pattern = [$"],
% Replacement = [$\\, $\\, $&],
% Escaped = re:replace(Binary, Pattern, Replacement, [{return, binary}, global]),
% ["\"", Escaped, "\""];
% --- %
% +++ %
EscapedWithAngles = io_lib:format("~p", [Binary]),
lists:nth(3, hd(EscapedWithAngles));
% +++ %

This avoids doing a pass for each type of escaped character and, at least for the obvious tests I threw at it, works. If I've missed a subtle edge-case, or used something about the returned value of io_lib:format I shouldn't, or otherwise committed an Erlang crime... please teach me!

from stdlib.

inoas avatar inoas commented on August 11, 2024

@DanielSherlock thanks I will take a look.

from stdlib.

DanielSherlock avatar DanielSherlock commented on August 11, 2024

Sorry, I took so long to type that out you'd basically fixed it in the mean time. I commented on the PR too, but after all that I like the clear way your solution reads and it means we don't have to worry about whether io_lib:format always returns something of the same shape.

from stdlib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.