1. anchorsearch

Tests whether a string begins with a specified prefix.

1.1. Syntax

string seek anchorsearch → post match true (if found)
                          → string false (if not found)

1.2. Stack Effects

Table 1. Before
Level Object

1

string (string to search in)

0

seek (prefix to search for)

Table 2. After (if match found)
Level Object

2

true

1

match (matching prefix portion)

0

post (remainder after prefix)

Table 3. After (if not found)
Level Object

1

false

0

string (original, unchanged)

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.4. PostScript Level

Level 1 and later

1.5. Examples

Successful prefix match
(abbc) (ab) anchorsearch
% Result: (bc) (ab) true
No match - wrong position
(abbc) (bb) anchorsearch
% Result: (abbc) false
% 'bb' exists but not at start
No match - not present
(abbc) (xyz) anchorsearch
% Result: (abbc) false
Case sensitivity
(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.6.2. Comment Stripping

line (%) anchorsearch {
  pop pop  % Discard comment
} {
  % Process line
} ifelse

1.6.3. File Extension Detection

filename (.ps) anchorsearch {
  % PostScript file
} {
  % Other file type
} 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

[invalidaccess]

String has no-access attribute

[stackoverflow]

Not enough room on stack for results

[stackunderflow]

Fewer than 2 operands on stack

[typecheck]

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


Back to top

Copyright © 2025 Ribose. PostScript is a trademark of Adobe. Distributed under the MIT License.