-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstring.vch
More file actions
executable file
·64 lines (55 loc) · 2.11 KB
/
string.vch
File metadata and controls
executable file
·64 lines (55 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
###
### Standard simple functions for manipulating strings
###
### Version 1.3
###
### cf. http://en.wikipedia.org/wiki/String_functions_(programming)
###
#
# Len: Returns the length of a string. An empty string returns a length of 0.
#
Len(string) := EvalTemplate('len(%s)', $string);
#
# Left(string,n): Returns the left n characters of a string. If n is
# greater than the length of the string then the whole string is
# returned. Behavior for negative values of n is undefined.
#
Left(string, count) := EvalTemplate('%s[0:%i]', $string, $count);
#
# Right(string,n): Returns the right n characters of a string. If n is
# greater than the length of the string then the whole string is
# returned. Behavior for negative values of n is undefined.
#
Right(string, count) := EvalTemplate('%s[-%i:]', $string, $count);
#
# Replace(string,from,to): replaces instances of $from in $string with
# $to from left to right with replacement text ineligible for further
# replacement; returns the resulting string.
#
# If $string contains no overlapping instances of $from, then this is
# equivalent to simultaneously replacing every occurrence of $from with $to.
#
# Examples:
#
# Replace(re**op*, *, **) = re****op**
# Replace(o***p, **, r*) = or**p
#
Replace(string, from, to) := EvalTemplate('%s.replace(%s, %s)',
$string, $from, $to);
#
# Split(string,separator,index): Splits $string by occurrences
# of $separator (itself a string) into a list of substrings then
# returns the $index-th(*) substring.
#
# Example splits:
# "ab,cd,,ef" by "," yields "ab", "cd", "", "ef"
# "ab//cd/e//f" by "//" yields "ab", "cd/e", "f"
# "" by "," yields ""
#
# (*) - 0 denotes the first string, 1 denotes the second string, ...
# -1 denotes the last string, -2 denotes the second to last string, ...
#
# Behavior for index values that describe nonexistent strings is undefined.
#
Split(string, separator, index) :=
EvalTemplate('%s.split(%s)[%i]', $string, $separator, $index);