1. anchorsearch
Tests whether a string begins with a specified prefix.
1.2. Stack Effects
| Level | Object |
|---|---|
1 |
|
0 |
|
| Level | Object |
|---|---|
2 |
|
1 |
|
0 |
|
| Level | Object |
|---|---|
1 |
|
0 |
|
1.3. Description
anchorsearch determines if string begins with seek (i.e., string is at least as long as seek and the initial characters match exactly).
If it matches:
* Splits string into match (the matching prefix) and post (the remainder)
* Pushes post, match, and true
If not:
* Pushes the original string and false
The match is case-sensitive and must occur at the start of string.
1.5. Examples
(abbc) (ab) anchorsearch
% Result: (bc) (ab) true
(abbc) (bb) anchorsearch
% Result: (abbc) false
% 'bb' exists but not at start
(abbc) (xyz) anchorsearch
% Result: (abbc) false
(Hello) (he) anchorsearch
% Result: (Hello) false ('he' != 'He')
1.6. Common Use Cases
1.6.1. Protocol Detection
url (http://) anchorsearch {
pop % post (rest of URL)
% Handle HTTP URL
} {
pop % original string
% Try other protocols
} ifelse
1.7. Common Pitfalls
Must Match At Start - anchorsearch only finds prefixes, not substrings anywhere in the string.
|
(hello) (ll) anchorsearch % false - not at start
| Case Sensitive - Matching is exact, including case. |
(PostScript) (post) anchorsearch % false
Use search for Anywhere - For substring search anywhere in string, use search instead.
|
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
String has no-access attribute |
[ |
Not enough room on stack for results |
[ |
Fewer than 2 operands on stack |
[ |
Either operand is not a string |
1.9. Implementation Notes
-
Very efficient - stops checking after first mismatch
-
Returns substring objects that share the original string value
-
Empty seek string always matches (returns entire string as post, empty match)
1.10. See Also
-
search- Search for substring anywhere -
token- Parse token from string -
getinterval- Extract substring