blob: 382fa0de448723c20a580c382d763d08451a4825 [file] [log] [blame]
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library analyzer.src.generated.java_core;
/**
* Inserts the given arguments into [pattern].
*
* format('Hello, {0}!', 'John') = 'Hello, John!'
* format('{0} are you {1}ing?', 'How', 'do') = 'How are you doing?'
* format('{0} are you {1}ing?', 'What', 'read') = 'What are you reading?'
*/
String format(String pattern,
[arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7]) {
// TODO(rnystrom): This is not used by analyzer, but is called by
// analysis_server. Move this code there and remove it from here.
return formatList(pattern, [arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7]);
}
/**
* Inserts the given [arguments] into [pattern].
*
* format('Hello, {0}!', ['John']) = 'Hello, John!'
* format('{0} are you {1}ing?', ['How', 'do']) = 'How are you doing?'
* format('{0} are you {1}ing?', ['What', 'read']) = 'What are you reading?'
*/
String formatList(String pattern, List<Object> arguments) {
if (arguments == null || arguments.isEmpty) {
assert(!pattern.contains(new RegExp(r'\{(\d+)\}')));
return pattern;
}
return pattern.replaceAllMapped(new RegExp(r'\{(\d+)\}'), (match) {
String indexStr = match.group(1);
int index = int.parse(indexStr);
Object arg = arguments[index];
assert(arg != null);
return arg?.toString();
});
}
class Character {
static const int MAX_VALUE = 0xffff;
static const int MAX_CODE_POINT = 0x10ffff;
static const int MIN_SUPPLEMENTARY_CODE_POINT = 0x010000;
static const int MIN_LOW_SURROGATE = 0xDC00;
static const int MIN_HIGH_SURROGATE = 0xD800;
static int digit(int codePoint, int radix) {
if (radix != 16) {
throw new ArgumentError("only radix == 16 is supported");
}
if (0x30 <= codePoint && codePoint <= 0x39) {
return codePoint - 0x30;
}
if (0x41 <= codePoint && codePoint <= 0x46) {
return 0xA + (codePoint - 0x41);
}
if (0x61 <= codePoint && codePoint <= 0x66) {
return 0xA + (codePoint - 0x61);
}
return -1;
}
static bool isDigit(int c) => c >= 0x30 && c <= 0x39;
static bool isLetter(int c) =>
c >= 0x41 && c <= 0x5A || c >= 0x61 && c <= 0x7A;
static bool isLetterOrDigit(int c) => isLetter(c) || isDigit(c);
static bool isWhitespace(int c) =>
c == 0x09 || c == 0x20 || c == 0x0A || c == 0x0D;
static String toChars(int codePoint) {
if (codePoint < 0 || codePoint > MAX_CODE_POINT) {
throw new ArgumentError();
}
if (codePoint < MIN_SUPPLEMENTARY_CODE_POINT) {
return new String.fromCharCode(codePoint);
}
int offset = codePoint - MIN_SUPPLEMENTARY_CODE_POINT;
int c0 = ((offset & 0x7FFFFFFF) >> 10) + MIN_HIGH_SURROGATE;
int c1 = (offset & 0x3ff) + MIN_LOW_SURROGATE;
return new String.fromCharCodes([c0, c1]);
}
}
abstract class Enum<E extends Enum> implements Comparable<E> {
/// The name of this enum constant, as declared in the enum declaration.
final String name;
/// The position in the enum declaration.
final int ordinal;
const Enum(this.name, this.ordinal);
int get hashCode => ordinal;
int compareTo(E other) => ordinal - other.ordinal;
String toString() => name;
}