refactor generateString to emit simpler dart code (#49334)
This commit is contained in:
committed by
GitHub
parent
94210a6632
commit
5a8de59a66
@@ -139,17 +139,17 @@ String _jsonToMap(dynamic json) {
|
||||
if (json == null || json is num || json is bool)
|
||||
return '$json';
|
||||
|
||||
if (json is String) {
|
||||
if (currentLocale == 'kn')
|
||||
return generateEncodedString(json);
|
||||
else if (json.contains("'"))
|
||||
return 'r"""$json"""';
|
||||
else
|
||||
return "r'''$json'''";
|
||||
}
|
||||
if (json is String)
|
||||
return generateEncodedString(currentLocale, json);
|
||||
|
||||
if (json is Iterable)
|
||||
return '<dynamic>[${json.map<String>(_jsonToMap).join(',')}]';
|
||||
if (json is Iterable) {
|
||||
final StringBuffer buffer = StringBuffer('<dynamic>[');
|
||||
for (final dynamic value in json) {
|
||||
buffer.writeln('${_jsonToMap(value)},');
|
||||
}
|
||||
buffer.write(']');
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
if (json is Map<String, dynamic>) {
|
||||
final StringBuffer buffer = StringBuffer('<String, dynamic>{');
|
||||
|
||||
@@ -464,10 +464,7 @@ String generateValue(String value, Map<String, dynamic> attributes, LocaleInfo l
|
||||
return _scriptCategoryToEnum[value];
|
||||
}
|
||||
}
|
||||
// Localization strings for the Kannada locale ('kn') are encoded because
|
||||
// some of the localized strings contain characters that can crash Emacs on Linux.
|
||||
// See packages/flutter_localizations/lib/src/l10n/README for more information.
|
||||
return locale.languageCode == 'kn' ? generateEncodedString(value) : generateString(value);
|
||||
return generateEncodedString(locale.languageCode, value);
|
||||
}
|
||||
|
||||
/// Combines [generateType], [generateKey], and [generateValue] to return
|
||||
|
||||
@@ -225,8 +225,8 @@ String genSimpleMethod(Message message) {
|
||||
for (final Placeholder placeholder in message.placeholders) {
|
||||
messageValue = messageValue.replaceAll('{${placeholder.name}}', '\${${placeholder.name}}');
|
||||
}
|
||||
final String rawMessage = generateString(messageValue); // "r'...'"
|
||||
return rawMessage.substring(1);
|
||||
final String generatedMessage = generateString(messageValue); // "r'...'"
|
||||
return generatedMessage.startsWith('r') ? generatedMessage.substring(1) : generatedMessage;
|
||||
}
|
||||
|
||||
List<String> genMethodParameters([String type]) {
|
||||
|
||||
@@ -371,46 +371,46 @@ String generateClassDeclaration(
|
||||
class $classNamePrefix$camelCaseName extends $superClass {''';
|
||||
}
|
||||
|
||||
/// Return `s` as a Dart-parseable raw string in single or double quotes.
|
||||
/// Return `s` as a Dart-parseable string.
|
||||
///
|
||||
/// Double quotes are expanded:
|
||||
/// The result tries to avoid character escaping:
|
||||
///
|
||||
/// ```
|
||||
/// foo => r'foo'
|
||||
/// foo "bar" => r'foo "bar"'
|
||||
/// foo 'bar' => r'foo ' "'" r'bar' "'"
|
||||
/// foo => 'foo'
|
||||
/// foo "bar" => 'foo "bar"'
|
||||
/// foo 'bar' => "foo 'bar'"
|
||||
/// foo 'bar' "baz" => '''foo 'bar' "baz"'''
|
||||
/// foo\bar => r'foo\bar'
|
||||
/// ```
|
||||
String generateString(String s) {
|
||||
if (!s.contains("'"))
|
||||
return "r'$s'";
|
||||
///
|
||||
/// Strings with newlines are not supported.
|
||||
String generateString(String value) {
|
||||
assert(!value.contains('\n'));
|
||||
final String rawPrefix = value.contains(r'$') || value.contains(r'\') ? 'r' : '';
|
||||
if (!value.contains("'"))
|
||||
return "$rawPrefix'$value'";
|
||||
if (!value.contains('"'))
|
||||
return '$rawPrefix"$value"';
|
||||
if (!value.contains("'''"))
|
||||
return "$rawPrefix'''$value'''";
|
||||
if (!value.contains('"""'))
|
||||
return '$rawPrefix"""$value"""';
|
||||
|
||||
final StringBuffer output = StringBuffer();
|
||||
bool started = false; // Have we started writing a raw string.
|
||||
for (int i = 0; i < s.length; i++) {
|
||||
if (s[i] == "'") {
|
||||
if (started)
|
||||
output.write("'");
|
||||
output.write(' "\'" ');
|
||||
started = false;
|
||||
} else if (!started) {
|
||||
output.write("r'${s[i]}");
|
||||
started = true;
|
||||
} else {
|
||||
output.write(s[i]);
|
||||
}
|
||||
}
|
||||
if (started)
|
||||
output.write("'");
|
||||
return output.toString();
|
||||
return value.split("'''")
|
||||
.map(generateString)
|
||||
// If value contains more than 6 consecutive single quotes some empty strings may be generated.
|
||||
// The following map removes them.
|
||||
.map((String part) => part == "''" ? '' : part)
|
||||
.join(" \"'''\" ");
|
||||
}
|
||||
|
||||
/// Only used to generate localization strings for the Kannada locale ('kn') because
|
||||
/// some of the localized strings contain characters that can crash Emacs on Linux.
|
||||
/// See packages/flutter_localizations/lib/src/l10n/README for more information.
|
||||
String generateEncodedString(String s) {
|
||||
if (s.runes.every((int code) => code <= 0xFF))
|
||||
return generateString(s);
|
||||
String generateEncodedString(String locale, String value) {
|
||||
if (locale != 'kn' || value.runes.every((int code) => code <= 0xFF))
|
||||
return generateString(value);
|
||||
|
||||
final String unicodeEscapes = s.runes.map((int code) => '\\u{${code.toRadixString(16)}}').join();
|
||||
final String unicodeEscapes = value.runes.map((int code) => '\\u{${code.toRadixString(16)}}').join();
|
||||
return "'$unicodeEscapes'";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user