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

add escape/unescape methods in String #9190

Closed
DartBot opened this issue Mar 15, 2013 · 9 comments
Closed

add escape/unescape methods in String #9190

DartBot opened this issue Mar 15, 2013 · 9 comments

Comments

@DartBot
Copy link

@DartBot DartBot commented Mar 15, 2013

This issue was originally filed by @tatumizer


Proposed two methods in String class:
String.unescape(),
String.escape()

E.g. print("abcd\t\c\u2578".unescape().escape())
will print
abcd\t\c\u2578

Native implementation of these methods will help to speed up some performance-critical functions (e.g. JsonParser.parse())

@DartBot
Copy link
Author

@DartBot DartBot commented Mar 15, 2013

This comment was originally written by @tatumizer


Sorry, already confused escape and unescape in example :) Don't know how to edit :)

@lrhn
Copy link
Member

@lrhn lrhn commented Mar 16, 2013

Am I understanding it correctly that you are asking for methods:
 escape: converts characters in string that are not valid in non-raw Dart string literals to String literal escapes for that character, and
 unescape: converts Dart string literal escape sequences to the character they represent
so that
  var s = r'\x41';
  print(escape(s)); // prints "\x41"
  print(unescape(s)); // prints "A"
?

As for any escaping/unescaping functions, there is no generic way to do specify it. It always depends on which setting the content is intended to be used in. In this case, I'm guessing it's Dart source code (but I could be wrong). That could make the methods unsuited for JSON, since the escapes are not the same. If it's just JSON escaping that is needed, you can use the JSON stringify and parse if you add or remove quotes manually.


Removed Type-Defect label.
Added Type-Enhancement, Area-Library, Triaged labels.

@DartBot
Copy link
Author

@DartBot DartBot commented Mar 16, 2013

This comment was originally written by @tatumizer


It as supposed to be mostly a performance improvement for JSON parsing,
assuming escape/unescape are implemented natively (important assumption).
I didn't notice that Dart escaping is not the same as JSONs
If so, "performance improvement" part is probably ruled out.
Another argument might be: "logical completeness". Dart has "parse" methods for several types, but not strings.
Probably these functions are better be called "escape" and "parseEscaped" (dart encoding).
But now my rationale for these methods is not very strong, I admit.

@lrhn
Copy link
Member

@lrhn lrhn commented Aug 23, 2013

The reason for int/double.parse is that you sometimes want to store integer and double data in a text (a String). Storing a String in a String is trivial, so we don't have extra functionality for it.
The fact that a string-representation of an int or double is also useful in other cases is a happy coincidence, and not something that is generally true for strings.
Different kinds of text escapes (which would probably fit in the new dart:convert library) could be implemented, but one that generates valid Dart string literals doesn't seem useful in general.


Added NotPlanned label.

@DartBot
Copy link
Author

@DartBot DartBot commented Aug 23, 2013

This comment was originally written by @tatumizer


I accept your argument with regard to the issue, as originally formulated.
However, since then dart introduced a concept of converters.
Would it be worthwhile to define a couple of "escape/unescape" converters?
I think it fits into "converters" paradigm AND provides extra example that
"validates" the utility of the concept itself.
How about that?

@floitschG
Copy link
Contributor

@floitschG floitschG commented Sep 2, 2013

Such converters would make sense but are currently not planned.

@DartBot
Copy link
Author

@DartBot DartBot commented Dec 2, 2013

This comment was originally written by @polux


I often need something like this for debugging. For instance, two strings aren't equal but it's hard to tell why because four spaces and one tab character look the same, or I print a tree with strings at the leaves but newlines mess up the layout.

@mleonhard
Copy link

@mleonhard mleonhard commented Feb 16, 2019

I need this, too, so my unit tests can print out strings that don't match. For now, I'm using the 'escape' and 'toPrintable' functions of the third-party 'strings' package:
https://pub.dartlang.org/packages/strings

@nxhiep
Copy link

@nxhiep nxhiep commented Apr 17, 2020

This issue was originally filed by @tatumizer

Proposed two methods in String class:
String.unescape(),
String.escape()

E.g. print("abcd\t\c\u2578".unescape().escape())
will print
abcd\t\c\u2578

Native implementation of these methods will help to speed up some performance-critical functions (e.g. JsonParser.parse())

I use this:
String covertStringToUnicode(String content) { String regex = "\\u"; int offset = content.indexOf(regex) + regex.length; while(offset > -1 + regex.length) { int limit = offset + 4; String str = content.substring(offset, limit); if(str != null && str.isNotEmpty){ String code = String.fromCharCode(int.parse(str, radix: 16)); content = content.replaceFirst(str, code, offset); } offset = content.indexOf(regex, limit) + regex.length; } return content.replaceAll(regex, ""); }
This works perfectly

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants