Untitled diff

Created Diff never expires
5 removals
27 lines
10 additions
29 lines
SQUEAK 5.1
PHARO 60437
ByteString>>findSubstring: key in: body startingAt: start matchTable: matchTable
String>>findSubstringViaPrimitive: key in: body startingAt: start matchTable: matchTable
"Answer the index in the string body at which the substring key first occurs, at or beyond start. The match is determined using matchTable, which can be used to effect, eg, case-insensitive matches. If no match is found, zero will be returned.
"Answer the index in the string body at which the substring key first occurs, at or beyond start. The match is determined using matchTable, which can be used to effect, eg, case-insensitive matches. If no match is found, zero will be returned.


The algorithm below is not optimum -- it is intended to be translated to C which will go so fast that it wont matter."
The algorithm below is not optimum -- it is intended to be translated to C which will go so fast that it wont matter."
| index |
| index |
<primitive: 'primitiveFindSubstring' module: 'MiscPrimitivePlugin'>
<primitive: 'primitiveFindSubstring' module: 'MiscPrimitivePlugin'>
<var: #key declareC: 'unsigned char *key'>
<var: #key declareC: 'unsigned char *key'>
<var: #body declareC: 'unsigned char *body'>
<var: #body declareC: 'unsigned char *body'>
<var: #matchTable declareC: 'unsigned char *matchTable'>
<var: #matchTable declareC: 'unsigned char *matchTable'>
key size = 0 ifTrue: [^ 0].
key size = 0 ifTrue: [^ 0].
(start max: 1) to: body size - key size + 1 do:
start to: body size - key size + 1 do:
[:startIndex |
[:startIndex |
index := 1.
index := 1.
[(matchTable at: (body basicAt: startIndex+index-1) + 1)
[(matchTable at: (body at: startIndex+index-1) asciiValue + 1)
= (matchTable at: (key basicAt: index) + 1)]
= (matchTable at: (key at: index) asciiValue + 1)]
whileTrue:
whileTrue:
[index = key size ifTrue: [^ startIndex].
[index = key size ifTrue: [^ startIndex].
index := index+1]].
index := index+1]].
^ 0
^ 0
"
"
' ' findSubstring: 'abc' in: 'abcdefabcd' startingAt: 1 matchTable: CaseSensitiveOrder 1
' ' findSubstring: 'abc' in: 'abcdefabcd' startingAt: 1 matchTable: CaseSensitiveOrder 1
' ' findSubstring: 'abc' in: 'abcdefabcd' startingAt: 2 matchTable: CaseSensitiveOrder 7
' ' findSubstring: 'abc' in: 'abcdefabcd' startingAt: 2 matchTable: CaseSensitiveOrder 7
' ' findSubstring: 'abc' in: 'abcdefabcd' startingAt: 8 matchTable: CaseSensitiveOrder 0
' ' findSubstring: 'abc' in: 'abcdefabcd' startingAt: 8 matchTable: CaseSensitiveOrder 0
' ' findSubstring: 'abc' in: 'abcdefABcd' startingAt: 2 matchTable: CaseSensitiveOrder 0
' ' findSubstring: 'abc' in: 'abcdefABcd' startingAt: 2 matchTable: CaseSensitiveOrder 0
' ' findSubstring: 'abc' in: 'abcdefABcd' startingAt: 2 matchTable: CaseInsensitiveOrder 7
' ' findSubstring: 'abc' in: 'abcdefABcd' startingAt: 2 matchTable: CaseInsensitiveOrder 7
"
"