shithub: purgatorio

Download patch

ref: 76f5318cbd58fbe2bfc8cb51856bfa217c673bb7
parent: ec35f468e0eba87c9f09cbbe5fa8af2591e6f914
author: henesy <devnull@localhost>
date: Mon May 31 19: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;
 };