PtcRunner.Lisp.Runtime.String (PtcRunner v0.9.0)

Copy Markdown View Source

String manipulation and parsing operations for PTC-Lisp runtime.

Provides string concatenation, substring, join, split, and parsing functions.

Summary

Functions

Convert string to lowercase.

Check if string ends with suffix.

Return lines matching the pattern (case-insensitive regex). String patterns are compiled as regex with BRE-to-PCRE translation (e.g. \| becomes | for alternation). Matching is case-insensitive by default.

Return lines matching the pattern with 1-based line numbers. String patterns are compiled as case-insensitive regex with BRE-to-PCRE translation.

Check if string contains substring.

Return the index of the first occurrence of value in s, or nil if not found. Optionally starts searching from a given index.

Join a collection into a string with optional separator.

Return the index of the last occurrence of value in s, or nil if not found. Optionally searches backwards from a given index.

Parse string to float. Returns nil on failure. Matches Clojure 1.11+ parse-double behavior.

Parse string to integer. Returns nil on failure. Matches Clojure 1.11+ parse-long behavior.

Return a readable string representation of zero or more values, space-separated.

Replace all occurrences of a pattern in a string.

Split a string by separator.

Split a string into a list of lines.

Check if string starts with prefix.

Convert zero or more values to string and concatenate.

Return substring starting at index (2-arity) or from start to end (3-arity).

Trim leading and trailing whitespace.

Convert string to uppercase.

Functions

downcase(s)

Convert string to lowercase.

  • (downcase "HELLO") returns "hello"
  • (downcase "") returns ""

ends_with?(s, suffix)

Check if string ends with suffix.

  • (ends-with? "hello" "lo") returns true
  • (ends-with? "hello" "x") returns false
  • (ends-with? "hello" "") returns true

grep(pattern, text)

Return lines matching the pattern (case-insensitive regex). String patterns are compiled as regex with BRE-to-PCRE translation (e.g. \| becomes | for alternation). Matching is case-insensitive by default.

  • (grep "error" text) returns lines containing "error", "Error", "ERROR", etc.
  • (grep "error\|warn" text) returns lines matching error or warn (any case)
  • (grep "" "a\nb") returns ["a", "b"] (empty pattern matches all)

grep_n(pattern, text, context \\ 0)

Return lines matching the pattern with 1-based line numbers. String patterns are compiled as case-insensitive regex with BRE-to-PCRE translation.

An optional context parameter (like grep -C) includes surrounding lines. Each result includes a :match boolean to distinguish matches from context.

  • (grep-n "error" text) returns [{:line 1 :text "error here" :match true} ...]
  • (grep-n "error" text 2) includes 2 lines of context around each match

includes?(s, substring)

Check if string contains substring.

  • (includes? "hello" "ll") returns true
  • (includes? "hello" "x") returns false
  • (includes? "hello" "") returns true

index_of(s, value)

Return the index of the first occurrence of value in s, or nil if not found. Optionally starts searching from a given index.

Uses grapheme indices (not byte offsets or UTF-16 code units) for consistency with subs, count, and other PTC-Lisp string functions.

  • (index-of "hello" "l") returns 2
  • (index-of "hello" "x") returns nil
  • (index-of "hello" "l" 3) returns 3
  • (index-of "hello" "" ) returns 0

index_of(s, value, from_index)

join(coll)

Join a collection into a string with optional separator.

  • (join ["a" "b" "c"]) returns "abc"
  • (join ", " ["a" "b" "c"]) returns "a, b, c"
  • (join "-" [1 2 3]) returns "1-2-3"
  • (join ", " []) returns ""

join(separator, coll)

last_index_of(s, value)

Return the index of the last occurrence of value in s, or nil if not found. Optionally searches backwards from a given index.

Correctly handles overlapping matches: (last-index-of "aaa" "aa") returns 1.

Uses grapheme indices (not byte offsets or UTF-16 code units) for consistency with subs, count, and other PTC-Lisp string functions.

  • (last-index-of "hello" "l") returns 3
  • (last-index-of "hello" "x") returns nil
  • (last-index-of "hello" "l" 2) returns 2
  • (last-index-of "hello" "") returns 5
  • (last-index-of "aaa" "aa") returns 1

last_index_of(s, value, from_index)

parse_double(s)

Parse string to float. Returns nil on failure. Matches Clojure 1.11+ parse-double behavior.

parse_long(s)

Parse string to integer. Returns nil on failure. Matches Clojure 1.11+ parse-long behavior.

pr_str_variadic(args)

Return a readable string representation of zero or more values, space-separated.

Like Clojure's pr-str, produces output suitable for reading back:

  • Strings get wrapped in quotes: (pr-str "hello")""hello""
  • nil becomes "nil": (pr-str nil)"nil"
  • Multiple args joined by space: (pr-str 1 "a")"1 "a""

replace(s, pattern, replacement)

Replace all occurrences of a pattern in a string.

  • (replace "hello" "l" "L") returns "heLLo"
  • (replace "aaa" "a" "b") returns "bbb"

split(s, re)

Split a string by separator.

  • (split "a,b,c" ",") returns ["a" "b" "c"]
  • (split "hello" "") returns ["h" "e" "l" "l" "o"]
  • (split "a,,b" ",") returns ["a" "" "b"]

split_lines(s)

Split a string into a list of lines.

  • (split-lines "line1 line2 line3") returns ["line1" "line2" "line3"]
  • Does not return trailing empty lines.

starts_with?(s, prefix)

Check if string starts with prefix.

  • (starts-with? "hello" "he") returns true
  • (starts-with? "hello" "x") returns false
  • (starts-with? "hello" "") returns true

str_variadic(args)

Convert zero or more values to string and concatenate.

Used as a :collect binding for the str builtin.

  • (str) returns ""
  • (str 42) returns "42"
  • (str "a" "b") returns "ab"
  • (str nil) returns "" (not "nil")
  • (str :keyword) returns ":keyword"
  • (str true) returns "true"

subs(s, start)

Return substring starting at index (2-arity) or from start to end (3-arity).

  • (subs "hello" 1) returns "ello"
  • (subs "hello" 1 3) returns "el"
  • (subs "hello" 0 0) returns ""
  • Out of bounds returns truncated result
  • Negative indices are clamped to 0

subs(s, start, end_idx)

to_str(s)

trim(s)

Trim leading and trailing whitespace.

  • (trim " hello ") returns "hello"
  • (trim " text ") returns "text"

upcase(s)

Convert string to uppercase.

  • (upcase "hello") returns "HELLO"
  • (upcase "") returns ""