Remove escape dollar parameter in localizations_utils (#54219)

This commit is contained in:
Shi-Hao Hong
2020-04-08 12:52:03 -07:00
committed by GitHub
parent 19c8948e61
commit 592f7886e5
2 changed files with 7 additions and 25 deletions

View File

@@ -387,24 +387,9 @@ class $classNamePrefix$camelCaseName extends $superClass {''';
/// foo\\nbar => 'foo\\\\nbar'
/// 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'
/// ```
///
/// 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);
String generateString(String value) {
const String backslash = '__BACKSLASH__';
assert(
!value.contains(backslash),
@@ -412,12 +397,12 @@ String generateString(String value, { bool escapeDollar = true }) {
'"__BACKSLASH__", as it is used as part of '
'backslash character processing.'
);
value = value.replaceAll('\\', backslash);
if (escapeDollar)
value = value.replaceAll('\$', '\\\$');
value = value
// Replace backslashes with a placeholder for now to properly parse
// other special characters.
.replaceAll('\\', backslash)
.replaceAll('\$', '\\\$')
.replaceAll("'", "\\'")
.replaceAll('"', '\\"')
.replaceAll('\n', '\\n')
@@ -425,6 +410,7 @@ String generateString(String value, { bool escapeDollar = true }) {
.replaceAll('\t', '\\t')
.replaceAll('\r', '\\r')
.replaceAll('\b', '\\b')
// Reintroduce escaped backslashes into generated Dart string.
.replaceAll(backslash, '\\\\');
return "'$value'";

View File

@@ -37,11 +37,7 @@ void main() {
});
test('escapes dollar when escapeDollar is true', () {
expect(generateString(r'ab$c', escapeDollar: true), "'ab\\\$c'");
});
test('does not escape dollar when escapeDollar is false', () {
expect(generateString(r'ab$c', escapeDollar: false), "'ab\$c'");
expect(generateString(r'ab$c'), "'ab\\\$c'");
});
test('handles backslash', () {