Unverified Commit 592f7886 authored by Shi-Hao Hong's avatar Shi-Hao Hong Committed by GitHub

Remove escape dollar parameter in localizations_utils (#54219)

parent 19c8948e
...@@ -387,24 +387,9 @@ class $classNamePrefix$camelCaseName extends $superClass {'''; ...@@ -387,24 +387,9 @@ class $classNamePrefix$camelCaseName extends $superClass {''';
/// foo\\nbar => 'foo\\\\nbar' /// foo\\nbar => 'foo\\\\nbar'
/// foo\\bar => 'foo\\\\bar' /// foo\\bar => 'foo\\\\bar'
/// foo\ bar => 'foo\\ bar' /// foo\ bar => 'foo\\ bar'
/// ```
///
/// When [shouldEscapeDollar] is set to true, the '$' characters in the
/// input will be replaced by '$' in the returned string:
/// ```
/// foo$bar = 'foo\$bar' /// foo$bar = 'foo\$bar'
/// ``` /// ```
/// String generateString(String value) {
/// When [shouldEscapeDollar] is set to false, '$' will be replaced
/// by '\$' in the returned string:
/// ```
/// foo$bar => 'foo\\\$bar'
/// ```
///
/// [shouldEscapeDollar] is true by default.
String generateString(String value, { bool escapeDollar = true }) {
assert(escapeDollar != null);
const String backslash = '__BACKSLASH__'; const String backslash = '__BACKSLASH__';
assert( assert(
!value.contains(backslash), !value.contains(backslash),
...@@ -412,12 +397,12 @@ String generateString(String value, { bool escapeDollar = true }) { ...@@ -412,12 +397,12 @@ String generateString(String value, { bool escapeDollar = true }) {
'"__BACKSLASH__", as it is used as part of ' '"__BACKSLASH__", as it is used as part of '
'backslash character processing.' 'backslash character processing.'
); );
value = value.replaceAll('\\', backslash);
if (escapeDollar)
value = value.replaceAll('\$', '\\\$');
value = value value = value
// Replace backslashes with a placeholder for now to properly parse
// other special characters.
.replaceAll('\\', backslash)
.replaceAll('\$', '\\\$')
.replaceAll("'", "\\'") .replaceAll("'", "\\'")
.replaceAll('"', '\\"') .replaceAll('"', '\\"')
.replaceAll('\n', '\\n') .replaceAll('\n', '\\n')
...@@ -425,6 +410,7 @@ String generateString(String value, { bool escapeDollar = true }) { ...@@ -425,6 +410,7 @@ String generateString(String value, { bool escapeDollar = true }) {
.replaceAll('\t', '\\t') .replaceAll('\t', '\\t')
.replaceAll('\r', '\\r') .replaceAll('\r', '\\r')
.replaceAll('\b', '\\b') .replaceAll('\b', '\\b')
// Reintroduce escaped backslashes into generated Dart string.
.replaceAll(backslash, '\\\\'); .replaceAll(backslash, '\\\\');
return "'$value'"; return "'$value'";
......
...@@ -37,11 +37,7 @@ void main() { ...@@ -37,11 +37,7 @@ void main() {
}); });
test('escapes dollar when escapeDollar is true', () { test('escapes dollar when escapeDollar is true', () {
expect(generateString(r'ab$c', escapeDollar: true), "'ab\\\$c'"); expect(generateString(r'ab$c'), "'ab\\\$c'");
});
test('does not escape dollar when escapeDollar is false', () {
expect(generateString(r'ab$c', escapeDollar: false), "'ab\$c'");
}); });
test('handles backslash', () { test('handles backslash', () {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment