Skip to content

Instantly share code, notes, and snippets.

@infinisil
Created August 29, 2019 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save infinisil/29aa5341646a13bd65b8a2f245b26f07 to your computer and use it in GitHub Desktop.
Save infinisil/29aa5341646a13bd65b8a2f245b26f07 to your computer and use it in GitHub Desktop.
with import <nixpkgs/lib>;
let
printf' =
result: # String
format: # [ Char ]
builtins.trace "printf' \"${result}\" \"${builtins.toJSON format}\"" (
if length format == 0 then result else
if head format == "%" then
let
type = head (tail format);
rest = tail (tail format);
in
if type == "s" then
string: printf' (result + string) rest
else if type == "d" then
number: printf' (result + toString number) rest
else if type == "%" then
printf' (result + "%") rest
else throw "Invalid format string"
else
printf' (result + head format) (tail format));
printf = format: printf' "" (stringToCharacters format);
in
printf "%%%ste%dst" "hi" 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment