[Gd-chatter] r11722 - in trunk/libraries/strings: . tests
cgay at gwydiondylan.org
cgay at gwydiondylan.org
Sat Mar 1 14:03:25 CET 2008
Author: cgay
Date: Sat Mar 1 14:03:25 2008
New Revision: 11722
Modified:
trunk/libraries/strings/library.dylan
trunk/libraries/strings/strings.dylan
trunk/libraries/strings/tests/library.dylan
trunk/libraries/strings/tests/strings-test-suite.dylan
Log:
job: minor
Remove replace functions, as regex-replace will do the same thing.
Modified: trunk/libraries/strings/library.dylan
==============================================================================
--- trunk/libraries/strings/library.dylan (original)
+++ trunk/libraries/strings/library.dylan Sat Mar 1 14:03:25 2008
@@ -7,7 +7,8 @@
define library strings
use common-dylan;
use io,
- import: { streams };
+ import: { streams,
+ format-out };
use string-extensions,
import: { string-hacking };
use regular-expressions;
@@ -59,8 +60,6 @@
create
substring,
trim,
- replace,
- replace!,
uppercase,
uppercase!,
lowercase,
@@ -85,6 +84,7 @@
define module strings-implementation
use strings; // Use API module
use common-dylan;
+ use format-out;
use streams,
import: { \with-output-to-string,
write,
Modified: trunk/libraries/strings/strings.dylan
==============================================================================
--- trunk/libraries/strings/strings.dylan (original)
+++ trunk/libraries/strings/strings.dylan Sat Mar 1 14:03:25 2008
@@ -39,14 +39,6 @@
define open generic trim (string :: <string>, #key) => (new-string :: <string>);
-define open generic replace
- (original :: <string>, pattern :: <object>, replacement :: <string>, #key)
- => (new-string :: <string>, num-replacements :: <integer>);
-
-define open generic replace!
- (original :: <string>, pattern :: <object>, replacement :: <string>, #key)
- => (string :: <string>, num-replacements :: <integer>);
-
// These three are much like their counterparts (=, >, <) but they provide
// for keyword args.
@@ -261,6 +253,8 @@
o1 = o2
end;
+// Note that if one of the strings "runs out" before the other
+// the result is #t.
define sealed method equal?
(string1 :: <byte-string>, string2 :: <byte-string>,
#key start1 :: <integer> = 0, end1 :: <integer> = string1.size,
@@ -717,7 +711,6 @@
end block
end method string-search-set;
-
define function check-base
(base :: <integer>) => ();
if (base < 2 | base > 36)
@@ -802,18 +795,18 @@
define sealed method trim
(string :: <byte-string>,
#key test :: <function> = whitespace?,
- side :: one-of(#"left", #"right", #"both") = #"both",
+ from :: one-of(#"left", #"right", #"both") = #"both",
start :: <integer> = 0,
end: _end :: <integer> = string.size)
=> (trimmed-string :: <byte-string>)
let bpos :: <integer> = start;
let epos :: <integer> = _end;
- if (side == #"both" | side == #"left")
+ if (from == #"both" | from == #"left")
while (bpos < epos & test(string[bpos]))
bpos := bpos + 1;
end;
end;
- if (side == #"both" | side == #"right")
+ if (from == #"both" | from == #"right")
while (bpos < (epos - 1) & test(string[epos - 1]))
epos := epos - 1;
end;
@@ -844,72 +837,6 @@
end method index-of;
-define sealed method replace
- (original :: <byte-string>, pattern :: <byte-string>, replacement :: <byte-string>,
- #key start :: <integer> = 0,
- end: _end :: <integer> = original.size,
- test :: <function> = \==,
- max: _max :: <integer> = -1)
- => (string :: <byte-string>, num-replacements :: <integer>)
- let psize :: <integer> = pattern.size;
- let substrings :: <stretchy-vector> = make(<stretchy-vector>);
- let bpos :: <integer> = start;
- let num-replacements :: <integer> = 0;
- let copy-from :: <integer> = 0;
- if (psize ~== 0)
- while (bpos <= _end - psize
- & (_max == -1 | num-replacements < _max))
- if (equal?(original, pattern, start1: bpos, end1: _end, test: test))
- if (copy-from ~== bpos)
- add!(substrings, copy-sequence(original, start: copy-from, end: bpos));
- end;
- add!(substrings, replacement);
- num-replacements := num-replacements + 1;
- bpos := bpos + psize;
- copy-from := bpos;
- else
- bpos := bpos + 1;
- end if;
- end while;
- end if;
- if (copy-from < original.size | empty?(substrings))
- add!(substrings, copy-sequence(original, start: copy-from));
- end;
- values(apply(concatenate, substrings), num-replacements)
-end method replace;
-
-define sealed method replace!
- (original :: <byte-string>, pattern :: <byte-string>, replacement :: <byte-string>,
- #key start :: <integer> = 0,
- end: _end :: <integer> = original.size,
- test :: <function> = \==,
- max: _max :: <integer> = -1)
- => (string :: <byte-string>, num-replacements :: <integer>)
- let psize :: <integer> = pattern.size;
- if (psize ~== replacement.size)
- replace(original, pattern, replacement,
- start: start, end: _end, test: test, max: _max)
- else
- let bpos :: <integer> = start;
- let epos :: <integer> = _end - psize;
- let num-replacements :: <integer> = 0;
- if (psize ~== 0) // prevent infinite loop
- while (bpos <= _end - psize
- & (_max == -1 | num-replacements < _max))
- if (equal?(original, pattern, start1: bpos, end1: _end, test: test))
- for (i from bpos, char in replacement)
- original[i] := char;
- end;
- num-replacements := num-replacements + 1;
- bpos := bpos + psize;
- end if;
- end while;
- end if;
- values(original, num-replacements)
- end if
-end method replace!;
-
-
define method substring
(string :: <byte-string>, start :: <integer>,
#key end: _end :: <integer> = string.size)
Modified: trunk/libraries/strings/tests/library.dylan
==============================================================================
--- trunk/libraries/strings/tests/library.dylan (original)
+++ trunk/libraries/strings/tests/library.dylan Sat Mar 1 14:03:25 2008
@@ -13,8 +13,7 @@
end;
define module strings-test-suite
- use common-dylan,
- exclude: { split };
+ use common-dylan;
use strings;
use testworks;
use testworks-specs;
Modified: trunk/libraries/strings/tests/strings-test-suite.dylan
==============================================================================
--- trunk/libraries/strings/tests/strings-test-suite.dylan (original)
+++ trunk/libraries/strings/tests/strings-test-suite.dylan Sat Mar 1 14:03:25 2008
@@ -58,14 +58,6 @@
open generic-function trim
(<string>, #"key", #"test", #"side", #"start", #"end") => (<string>);
- open generic-function replace
- (<string>, <string>, <string>, #"key", #"start", #"end", #"test", #"max")
- => (<string>, <integer>);
-
- open generic-function replace!
- (<string>, <string>, <string>, #"key", #"start", #"end", #"test", #"max")
- => (<string>, <integer>);
-
open generic-function uppercase (<string>) => (<string>);
open generic-function uppercase! (<string>) => (<string>);
open generic-function lowercase (<string>) => (<string>);
@@ -188,9 +180,9 @@
for (item in list(#("", ""),
#("a", "a"),
#("a", " a "),
- #("a", " a ", side:, #"both"), // same
- #("a ", " a ", side:, #"left"),
- #(" a", " a ", side:, #"right"),
+ #("a", " a ", from:, #"both"), // same
+ #("a ", " a ", from:, #"left"),
+ #(" a", " a ", from:, #"right"),
list(" a ", " a ", test:, method (c) #f end),
list("o", "xox", test:, method (c) c == 'x' end)
))
@@ -231,57 +223,30 @@
//---*** Fill this in...
end function-test digit-to-integer;
-define function replacement-test (mutating?)
- for (item in list(list("", "", "", "", #[]),
- list("", "", "", "replacement", #[]),
- list("b", "a", "a", "b", #[]),
- list("a", "a", "a", "b", #[#"end", 0]),
- list("ac", "abc", "b", "", #[]),
- list("axxc", "abc", "b", "xx", #[]),
- list("abc", "abc", "b", "xx", #[#"start", 2])
- /*
- list("abc", "abc", "b", "xx", #[#"end", 1]),
- list("xxa", "aaa", "a", "x", #[#"max", 2]),
- list("AxA", "AaA", "a", "x", #[]),
- list("xxx", "AaA", "a", "x", vector(#"test", case-insensitive-equal?))
- */
- ))
- let (expected, original, pattern, replacement, kwargs) = apply(values, item);
- let original = copy-sequence(original);
- let test-name = fmt("replace%s(%=, %=, %=, %=)",
- if (mutating?) "!" else "" end,
- original, pattern, replacement, kwargs);
- let result = block ()
- apply(if (mutating?)
- replace!
- else
- replace
- end,
- original, pattern, replacement, kwargs);
- exception (e :: <error>)
- #f
- end;
- check-equal(test-name, original, result);
- if (pattern.size == replacement.size)
- check-true(fmt("%s identity", test-name), result == original);
- end;
- end for;
-end function replacement-test;
-
-define strings function-test replace ()
- replacement-test(#f);
-end function-test replace;
-
-define strings function-test replace! ()
- replacement-test(#t);
-end function-test replace!;
-
define strings function-test less? ()
//---*** Fill this in...
end function-test less?;
define strings function-test equal? ()
- //---*** Fill this in...
+ local method e (s1, s2, #rest args)
+ check-true(fmt("equal?(%=, %=, ,@%=)", s1, s2, args),
+ apply(equal?, s1, s2, args))
+ end;
+ e("", "");
+ e("abc", "abc");
+ e("xaaax", "yaaay", start1: 1, end1: 4, start2: 1, end2: 4);
+ e("a", "", end1: 0);
+ e("a", "", start1: 1);
+ e("", "a", end2: 0);
+ e("", "a", start2: 1);
+ e("abcd", "ab", end1: 2);
+ e("abcd", "ab", end1: 1, end2: 1);
+ e("abcd", "bc", start1: 1, end1: 3);
+ e("abcd", "cd", start1: 2);
+ e("ab", "abcd", end2: 2);
+ e("ab", "abcd", end1: 1, end2: 1);
+ e("bc", "abcd", start2: 1, end2: 3);
+ e("cd", "abcd", start2: 2);
end function-test equal?;
define strings function-test control? ()
More information about the chatter
mailing list