From 5907ec3bb5d21f19c7f1ab34f57b5b661310c062 Mon Sep 17 00:00:00 2001 From: Alix Lourme Date: Tue, 19 Jul 2022 14:08:06 +0200 Subject: [PATCH] Fix #70 : getfield implementation splits only on character and not str --- src/global.tcl | 7 ++++++- test/test_global.tcl | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/global.tcl b/src/global.tcl index 56f0738..fc6af4b 100644 --- a/src/global.tcl +++ b/src/global.tcl @@ -123,7 +123,12 @@ namespace eval ::testcl { proc ::testcl::getfield { str delim ind } { log::log debug "GLOBAL::getfield $str $delim $ind invoked" - return [lindex [split $str $delim] [expr {$ind - 1}]] + if { [string len $delim] == 1 } { + return [lindex [split $str $delim] [expr {$ind - 1}]] + } + # See https://gist.github.com/ncimino/5526288 + set mc \x00 + return [lindex [split [string map [list $delim $mc] $str] $mc] [expr {$ind - 1}]] } # testcl::log -- diff --git a/test/test_global.tcl b/test/test_global.tcl index 3d56f89..ce78458 100644 --- a/test/test_global.tcl +++ b/test/test_global.tcl @@ -11,3 +11,12 @@ assertStringEquals [getfield "foo:bar" ":" 0] "" assertStringEquals [getfield "foo:bar" ":" 1] "foo" assertStringEquals [getfield "foo:bar" ":" 2] "bar" assertStringEquals [getfield "foo:bar" ":" 3] "" + +assertStringEquals [getfield "st-str-ts" "str" -1] "" +assertStringEquals [getfield "st-str-ts" "str" 0] "" +assertStringEquals [getfield "st-str-ts" "str" 1] "st-" +assertStringEquals [getfield "st-str-ts" "str" 2] "-ts" +assertStringEquals [getfield "st-str-ts" "str" 3] "" +assertStringEquals [getfield "st-str-ts" "str" 42] "" + +assertStringEquals [getfield "st-str-ts-str-foo" "str" 3] "-foo" \ No newline at end of file