Skip to content

Support escaping and unescape-ing strings in a general way #99

Description

@LPeter1997

Is your feature request related to a problem? Please describe.
Compilers and tooling require a lot of annoying string processing, one of which is unescaping the escape sequences in strings. This is usually required for parsed string literals and regexes. This involves turning \n, \r, ... into the appropriate characters.

Sometimes, the opposite is required, when a string is given, and the user wants the escaped representation.

Describe the solution you'd like
We should provide two methods, one for escaping and one for unescaping strings. Yoakke.Text seems like the ideal place to put it. For example, Unescape("\\thello\\n") should result in "\thello\n" and Escape("\thello\n") should result in "\\thello\\n".

Somehow we should also annotate, which escapes we want to allow (maybe even custom escape sequences, can be really useful for lexing custom formats). Also, supporting unicode codepoint escapes would be nice, just like \Unnnn or \unnnnnnnn

Describe alternatives you've considered
The API is not clear in my head yet, but 2 basic signatures could be:

  • StringEscapes.Escape(ReadOnlySpan<char>)
  • StringEscapes.Unescape(ReadOnlySpan<char>)
    This does not yet detail, how we want the feature to be customizable, I haven't thought of it yet.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions