-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#80: Attempt at abstracting over String, to be able to use Characters.
- Loading branch information
Showing
47 changed files
with
320 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/// This package contains the parse input buffers. | ||
import 'package:characters/characters.dart'; | ||
|
||
import 'src/buffer/character_buffer.dart'; | ||
import 'src/buffer/string_buffer.dart'; | ||
|
||
abstract class Buffer { | ||
/// Create a buffer from the most appropriate internal implementation. | ||
factory Buffer(/*Buffer|String|Characters*/ dynamic input) { | ||
if (input is Buffer) { | ||
return input; | ||
} else if (input is String) { | ||
return Buffer.fromString(input); | ||
} else if (input is Characters) { | ||
return Buffer.fromCharacters(input); | ||
} else { | ||
throw ArgumentError.value(input, 'input', 'Invalid input buffer'); | ||
} | ||
} | ||
|
||
/// Create a buffer from an input [String] (UTF-16). | ||
factory Buffer.fromString(String input) => StringBuffer(input); | ||
|
||
/// Create a buffer from input [Characters] (Unicode Grapheme). | ||
factory Buffer.fromCharacters(Characters input) => CharactersBuffer(input); | ||
|
||
/// Return the length of the buffer. | ||
int get length; | ||
|
||
/// Return the character at [position]. | ||
String charAt(int position); | ||
|
||
/// Return the character code at [position]. | ||
int codeUnitAt(int position); | ||
|
||
/// Return the substring between [start] and [stop] (exclusive). | ||
String substring(int start, int stop); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import 'package:characters/characters.dart'; | ||
|
||
import '../../buffer.dart'; | ||
|
||
class CharactersBuffer implements Buffer { | ||
final List<String> characters; | ||
|
||
CharactersBuffer(Characters characters) | ||
: characters = characters.toList(growable: false); | ||
|
||
@override | ||
int get length => characters.length; | ||
|
||
@override | ||
String charAt(int position) => characters[position]; | ||
|
||
@override | ||
int codeUnitAt(int position) { | ||
final chars = characters[position]; | ||
final charsLength = chars.length; | ||
if (charsLength == 1) { | ||
return chars.codeUnitAt(0); | ||
} else { | ||
var value = 0; | ||
for (var i = 0; i < charsLength; i++) { | ||
value = (value << 16) | chars.codeUnitAt(i); | ||
} | ||
return value; | ||
} | ||
} | ||
|
||
@override | ||
String substring(int start, int end) => | ||
characters.getRange(start, end).join(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import 'package:meta/meta.dart'; | ||
|
||
import '../../buffer.dart'; | ||
|
||
@immutable | ||
class StringBuffer implements Buffer { | ||
final String string; | ||
|
||
const StringBuffer(this.string); | ||
|
||
@override | ||
int get length => string.length; | ||
|
||
@override | ||
String charAt(int position) => string[position]; | ||
|
||
@override | ||
int codeUnitAt(int position) => string.codeUnitAt(position); | ||
|
||
@override | ||
String substring(int start, int end) => string.substring(start, end); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import '../../buffer.dart'; | ||
import '../core/parser.dart'; | ||
|
||
extension AcceptParser<T> on Parser<T> { | ||
/// Tests if the [input] can be successfully parsed. | ||
/// | ||
/// For example, `letter().plus().accept('abc')` returns `true`, and | ||
/// `letter().plus().accept('123')` returns `false`. | ||
bool accept(String input) => fastParseOn(input, 0) >= 0; | ||
bool accept(/*Buffer|String|Characters*/ dynamic input) => | ||
fastParseOn(Buffer(input), 0) >= 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.