shithub: 9ferno

Download patch

ref: da7d6df6faf18e289fe0f3f61524dcc7fddeef18
parent: a273f86715fb659397106572765870effc33622e
author: henesy <unknown>
date: Tue Jun 1 02:46:45 EDT 2021

string.m: add fields

--- a/appl/lib/string.b
+++ b/appl/lib/string.b
@@ -1,5 +1,6 @@
 implement String;
 
+include "sys.m";
 include "string.m";
 
 splitl(s: string, cl: string): (string, string)
@@ -496,7 +497,7 @@
 	return in;
 }
 
-# Returns >0 if s∈in and <=0 if s∋in
+# Returns >0 if s∈in and ≤0 if s∋in
 contains(in, s: string): int {
 	if(in == "" || s == "") {
 		# Can't do anything
@@ -527,3 +528,27 @@
 	
 	return 0;
 }
+
+# Return whitespace-delimited elements
+fields(s: string): list of string {
+	sys := load Sys Sys->PATH;
+	out: list of string;
+	
+	word := "";
+	for(i := 0; i < len s; i++) {
+		case s[i] {
+		' ' or '	' or '\n' =>
+			if(word != ""){
+				out = append(word, out);
+				word = "";
+			}
+		* =>
+			word += sys->sprint("%c", s[i]);
+		}
+	}
+	if(word != ""){
+		out = append(word, out);
+	}
+	return out;
+}
+
--- a/module/string.m
+++ b/module/string.m
@@ -39,4 +39,5 @@
 	quoted:		fn(argv: list of string): string;
 	quotedc:		fn(argv: list of string, cl: string): string;
 	unquoted:		fn(args: string): list of string;
+	fields:		fn(s: string): list of string;
 };