Perf update
This commit is contained in:
parent
0ae03592fb
commit
13cd62a6f0
4 changed files with 96 additions and 6 deletions
|
@ -2,6 +2,7 @@ library angel_route.src.router;
|
|||
|
||||
import 'extensible.dart';
|
||||
import 'routing_exception.dart';
|
||||
import '../string_util.dart';
|
||||
part 'symlink_route.dart';
|
||||
part 'route.dart';
|
||||
part 'routing_result.dart';
|
||||
|
@ -266,8 +267,8 @@ class Router {
|
|||
/// with the given method.
|
||||
RoutingResult resolve(String absolute, String relative,
|
||||
{String method: 'GET'}) {
|
||||
final cleanAbsolute = absolute.replaceAll(_straySlashes, '');
|
||||
final cleanRelative = relative.replaceAll(_straySlashes, '');
|
||||
final cleanAbsolute = stripStraySlashes(absolute);
|
||||
final cleanRelative = stripStraySlashes(relative);
|
||||
final segments = cleanRelative.split('/').where((str) => str.isNotEmpty);
|
||||
//print(
|
||||
// 'Now resolving $method "/$cleanRelative", absolute: $cleanAbsolute');
|
||||
|
@ -283,9 +284,9 @@ class Router {
|
|||
|
||||
if (match != null) {
|
||||
final cleaned = s.join('/').replaceFirst(match[0], '');
|
||||
final tail = cleanRelative
|
||||
.replaceAll(route._head, '')
|
||||
.replaceAll(_straySlashes, '');
|
||||
var tail = cleanRelative
|
||||
.replaceAll(route._head, '');
|
||||
tail = stripStraySlashes(tail);
|
||||
|
||||
if (cleaned.isEmpty) {
|
||||
//print(
|
||||
|
|
43
lib/string_util.dart
Normal file
43
lib/string_util.dart
Normal file
|
@ -0,0 +1,43 @@
|
|||
/// Helper functions to performantly transform strings, without `RegExp`.
|
||||
library angel_route.string_util;
|
||||
|
||||
/// Removes leading and trailing occurrences of a pattern from a string.
|
||||
String stripStray(String haystack, String needle) {
|
||||
int firstSlash;
|
||||
|
||||
if (haystack.startsWith(needle)) {
|
||||
firstSlash = haystack.indexOf(needle);
|
||||
if (firstSlash == -1) return haystack;
|
||||
} else {
|
||||
firstSlash = -1;
|
||||
}
|
||||
|
||||
if (firstSlash == haystack.length - 1)
|
||||
return haystack.length == 1 ? '' : haystack.substring(0, firstSlash);
|
||||
|
||||
// Find last leading index of slash
|
||||
for (int i = firstSlash + 1; i < haystack.length; i++) {
|
||||
if (haystack[i] != needle) {
|
||||
var sub = haystack.substring(i);
|
||||
|
||||
if (!sub.endsWith(needle))
|
||||
return sub;
|
||||
|
||||
var lastSlash = sub.lastIndexOf(needle);
|
||||
|
||||
for (int j = lastSlash - 1; j >= 0; j--) {
|
||||
if (sub[j] != needle) {
|
||||
return sub.substring(0, j + 1);
|
||||
}
|
||||
}
|
||||
|
||||
return lastSlash == -1 ? sub : sub.substring(0, lastSlash);
|
||||
}
|
||||
}
|
||||
|
||||
return haystack.substring(0, firstSlash);
|
||||
}
|
||||
|
||||
String stripStraySlashes(String str) => stripStray(str, '/');
|
||||
|
||||
String stripRegexStraySlashes(String str) => stripStray(str, '\\/');
|
|
@ -1,6 +1,6 @@
|
|||
name: angel_route
|
||||
description: A powerful, isomorphic routing library for Dart.
|
||||
version: 1.0.6
|
||||
version: 1.0.7
|
||||
author: Tobe O <thosakwe@gmail.com>
|
||||
homepage: https://github.com/angel-dart/angel_route
|
||||
dev_dependencies:
|
||||
|
|
46
test/strip_test.dart
Normal file
46
test/strip_test.dart
Normal file
|
@ -0,0 +1,46 @@
|
|||
import 'package:angel_route/string_util.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
main() {
|
||||
test('strip leading', () {
|
||||
var a = '///a';
|
||||
var b = stripStraySlashes(a);
|
||||
print('$a => $b');
|
||||
expect(b, 'a');
|
||||
});
|
||||
|
||||
test('strip trailing', () {
|
||||
var a = 'a///';
|
||||
var b = stripStraySlashes(a);
|
||||
print('$a => $b');
|
||||
expect(b, 'a');
|
||||
});
|
||||
|
||||
test('strip both', () {
|
||||
var a = '///a///';
|
||||
var b = stripStraySlashes(a);
|
||||
print('$a => $b');
|
||||
expect(b, 'a');
|
||||
});
|
||||
|
||||
test('intermediate slashes preserved', () {
|
||||
var a = '///a///b//';
|
||||
var b = stripStraySlashes(a);
|
||||
print('$a => $b');
|
||||
expect(b, 'a///b');
|
||||
});
|
||||
|
||||
test('only if starts with', () {
|
||||
var a = 'd///a///b//';
|
||||
var b = stripStraySlashes(a);
|
||||
print('$a => $b');
|
||||
expect(b, 'd///a///b');
|
||||
});
|
||||
|
||||
test('only if ends with', () {
|
||||
var a = '///a///b//c';
|
||||
var b = stripStraySlashes(a);
|
||||
print('$a => $b');
|
||||
expect(b, 'a///b//c');
|
||||
});
|
||||
}
|
Loading…
Reference in a new issue