ref: 80be5158599a247dff7f7701cc883c118b15eaa6
parent: 5635aa2a9d090b1b3465b7af76997a2198b0a471
author: henesy <unknown>
date: Sun Dec 13 17:22:23 EST 2020
string(2): add contains()
--- a/appl/lib/string.b
+++ b/appl/lib/string.b
@@ -495,3 +495,39 @@
return in;
}
+
+# Returns >0 if s∈in and <=0 if s∋in
+contains(in, s: string): int {
+ if(s == "") {
+ # Of course
+ return 1;
+ }
+ if(len in < 1) {
+ # Can't have anything
+ return 0;
+ }
+
+ # For each rune 'in' the original string
+ for(r0 := 0; r0 < len in ; r0++) {
+ # Try to build 's'
+ r1: int;
+
+ substring:
+ for(r1 = 0; r1 < len s; r1++) {
+ if(in[r0 + r1] == s[r1]) {
+ # Match so far
+ continue substring;
+ } else {
+ # No longer matches
+ break substring;
+ }
+ }
+
+ # We didn't break the loop, this is a match
+ # String will be [r0 , r0+len(s))
+ if(r1 >= len s)
+ return 1;
+ }
+
+ return 0;
+}
--- a/man/2/string
+++ b/man/2/string
@@ -14,6 +14,7 @@
splitl: fn(s, cl: string): (string, string);
splitr: fn(s, cl: string): (string, string);
replace: fn(in, s, with: string, max: int): string;
+contains: fn(in, s: string): int;
splitstrl: fn(s, t: string): (string, string);
splitstrr: fn(s, t: string): (string, string);
take: fn(s, cl: string): string;
@@ -134,6 +135,14 @@
the string
.I in
is returned unchanged.
+
+.PP
+.B Contains
+returns >0 if
+.I s
+is contained within
+.I in
+and ≤0 otherwise.
.PP
.B Take
--- a/module/string.m
+++ b/module/string.m
@@ -21,6 +21,7 @@
splitstrl: fn(s, t: string): (string, string);
splitstrr: fn(s, t: string): (string, string);
replace: fn(in, s, with: string, max: int): string;
+ contains: fn(in, s: string): int;
# is first arg a prefix of second?
prefix: fn(pre, s: string): int;