Skip to content

Instantly share code, notes, and snippets.

@jeandudey
Created August 10, 2015 21:04
Show Gist options
  • Save jeandudey/df4b9f520997785e74ac to your computer and use it in GitHub Desktop.
Save jeandudey/df4b9f520997785e74ac to your computer and use it in GitHub Desktop.
String to char literals (to use it with PEGTL).
#!/usr/bin/python3
# Description: convert an string to a "literal string" (i.e. 'H', 'e', 'l', 'l', 'o')
# to use in PEGTL (i.e pegtl::string< 'H', 'e', 'l', 'l', 'o', ',', ' ' >).
def toliteralstring(str):
result = ""
for c in str:
result += "'" + c "' "
result = result[:-2]
return result
strings = ["bool", "int"]
# Output:
# 'b', 'o', 'o', 'l'
# 'i', 'n', 't'
for string in strings:
print(toliteralstring(string))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment