some code

Jeff Cutsinger seaslug at tunes.org
Tue Jul 20 22:25:32 PDT 2004


I've written a couple of extensions to the slate standard libs. They aren't
thoroughly tested. They aren't commented, but they seem self explanatory to me.

r@(ReadStream traits) upTo: c
[| char string |
  char: r next.
  string: ''.
  [ ((char = c) \/ (r atEnd)) not ] whileTrue:
    [ string: string ; { char }.
      char: r next ].
  (char = c)
    ifTrue: [string]
    ifFalse: [Nil]
].

r@(ReadStream traits) upToOneOf: c@(Collection traits)
[| char string |
  char: r next.
  string: ''.
  [ ((c includes: char) \/ (r atEnd)) not ] whileTrue:
    [ string: string ; { char }.
      char: r next ].
  (char = c)
    ifTrue: [string]
    ifFalse: [Nil]
].

r@(ReadStream traits) upTo: s@(String traits)
[| string sIndex |
  string: (r upTo: s first).
  string
    ifNil: [sIndex: -1]
    ifNotNil: [sIndex: 1].
  [ (r atEnd) not /\ (sIndex ~= s size) ] whileTrue:
    [ | char |
      char: r next.
      string: string ; { char }.
      ((s at: sIndex) = char)
        ifTrue: [ sIndex: sIndex + 1 ]
        ifFalse: [ sIndex: 0 ]. ].
  (sIndex = s size)
    ifTrue: [string copyFrom: 0 to: (string size - s size)]
    ifFalse: [Nil]
].

"To go in string.slate"

s@(String traits) stripOneOf: c@(Collection traits) startingAt: start
[ | new |
  new: (s copyFrom: 0 to: (start - 1)).
  start below: s size
    do: [ | :index |
          (c includes: (s at: index))
            ifFalse: [ new: new ; { (s at: index) }]].
  new
].

s@(String traits) stripOneOf: c@(Collection traits)
[ s stripOneOf: c startingAt: 0 ].

s@(String traits) strip: c@(Character traits) startingAt: start
[ s stripOneOf: {c} startingAt: start ].

s@(String traits) strip: c@(Character traits)
[ s stripOneOf: {c} startingAt: 0 ].

s@(String traits) stripStartingAt: start
[ s stripOneOf: { $\s. $\n. $\t. $\r. $\0 } startingAt: start ].

s@(String traits) strip
[ s stripOneOf: { $\s. $\n. $\t. $\r. $\0 } startingAt: 0].

-seaslug



More information about the Slate mailing list