Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(101)

Side by Side Diff: pkg/analysis_services/lib/src/correction/strings.dart

Issue 475543002: New analyzer snapshot, add format/formatList. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library services.src.correction.strings; 5 library services.src.correction.strings;
6 6
7 7
8 /** 8 /**
9 * "$" 9 * "$"
10 */ 10 */
(...skipping 24 matching lines...) Expand all
35 if (a == null) { 35 if (a == null) {
36 return 1; 36 return 1;
37 } 37 }
38 if (b == null) { 38 if (b == null) {
39 return -1; 39 return -1;
40 } 40 }
41 return a.compareTo(b); 41 return a.compareTo(b);
42 } 42 }
43 43
44 /** 44 /**
45 * Inserts the given arguments into [pattern].
46 *
47 * format('Hello, {0}!', 'John') = 'Hello, John!'
48 * format('{0} are you {1}ing?', 'How', 'do') = 'How are you doing?'
49 * format('{0} are you {1}ing?', 'What', 'read') = 'What are you reading?'
50 */
51 String format(String pattern, [arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7])
52 {
53 return formatList(pattern, [arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7]);
54 }
55
56 /**
57 * Inserts the given [args] into [pattern].
58 *
59 * format('Hello, {0}!', ['John']) = 'Hello, John!'
60 * format('{0} are you {1}ing?', ['How', 'do']) = 'How are you doing?'
61 * format('{0} are you {1}ing?', ['What', 'read']) = 'What are you reading?'
62 */
63 String formatList(String pattern, List args) {
64 return pattern.replaceAllMapped(new RegExp(r'\{(\d+)\}'), (match) {
65 String indexStr = match.group(1);
66 int index = int.parse(indexStr);
67 var arg = args[index];
68 return arg != null ? arg.toString() : null;
69 });
70 }
71
72 /**
73 * Checks if [str] is `null`, empty or is whitespace. 45 * Checks if [str] is `null`, empty or is whitespace.
74 */ 46 */
75 bool isBlank(String str) { 47 bool isBlank(String str) {
76 if (str == null) { 48 if (str == null) {
77 return true; 49 return true;
78 } 50 }
79 if (str.isEmpty) { 51 if (str.isEmpty) {
80 return true; 52 return true;
81 } 53 }
82 return str.codeUnits.every(isSpace); 54 return str.codeUnits.every(isSpace);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 return str; 101 return str;
130 } 102 }
131 103
132 String repeat(String s, int n) { 104 String repeat(String s, int n) {
133 StringBuffer sb = new StringBuffer(); 105 StringBuffer sb = new StringBuffer();
134 for (int i = 0; i < n; i++) { 106 for (int i = 0; i < n; i++) {
135 sb.write(s); 107 sb.write(s);
136 } 108 }
137 return sb.toString(); 109 return sb.toString();
138 } 110 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698