Browse Source

strtok tokenizer

Jaromil 4 years ago
parent
commit
c865ff0deb
1 changed files with 32 additions and 0 deletions
  1. 32 0
      zuper

+ 32 - 0
zuper

@@ -257,6 +257,31 @@ _ztmp_destructor() {
257 257
 arrs+=(tmpfiles)
258 258
 destruens+=(_ztmp_destructor)
259 259
 
260
+# tokenizer, works only with one char length delimiters
261
+# saves everything in global array tok=()
262
+arrs+=(tok)
263
+strtok() {
264
+    fn "strtok $*"
265
+    _string="$1"
266
+    _delim="$2"
267
+    req=(_string _delim)
268
+    ckreq || return $?
269
+
270
+    tok=()
271
+    f=0
272
+    for c in {1..${#_string}}; do
273
+        if [[ "${_string[(e)$c]}" == "$_delim" ]]; then
274
+            # check if not empty
275
+            t=${_string[(e)$(($f + 1)),$(($c - 1))]}
276
+            [[ "$t" == "" ]] || tok+=($t)
277
+            # save last found
278
+            f=$c
279
+        fi
280
+    done
281
+    # add last token
282
+    t=${_string[(e)$(($f + 1)),$c]}
283
+    [[ "$t" == "" ]] || tok+=($t)
284
+}
260 285
 
261 286
 # optional: define zkv=1 on source
262 287
 
@@ -530,6 +555,13 @@ s/"/\"/g
530 555
 
531 556
 # {{{ Config
532 557
 
558
+# This is not a full config parser, but its a mechanism to read single
559
+# sections of configuration files that are separated using various
560
+# syntax methods. The only method supported is now org-mode whose
561
+# sections start with #+ . It fills in the global array
562
+# $config_section which can be read out to a file or interpreted in
563
+# memory, whatever syntax it may contain.
564
+
533 565
 vars+=(config_section_type)
534 566
 arrs+=(config_section)
535 567
 config_section_type=org-mode