/**************************************************************************** Kalkulon - A programmable calculator for programmers This is an example Kalkulon script. To load from within Kalkulon: Load("examples/scriptname.k") then you can call functions like: funcname() or funcname(arg0, ...) Author: Juergen Holetzeck 2003 - 2009 e-mail: contact@kalkulon.de homepage: www.kalkulon.de ****************************************************************************/ //////////////////////////////////////////////////////////////////////////// // some useful functions for manipulating strings // find substr in string or sublist in list // returns: pos if found; -1 otherwise // example 1: find("def", "abcdefghi") -> 3 // example 2: find({1,2}, {3,4,5,1,2,5,6}) -> 3 find(substr, str) = find(substr, str, 0); find(substr, str, pos) = ( lenSub = Size(substr), len = Size(str), ok = 0, while(!ok && pos+lenSub <= len; if(Sub(str, pos, pos+lenSub) == substr; ok = 1; ++pos)), if(ok; pos; -1) ); // decompose string at delimiter // returns: list of sub strings // example: decompose("ABC DEF GHI") -> {"ABC", "DEF", "GHI"} decompose(str) = decompose(str, " "); decompose(str, delim) = ( result={}, beg = end = 0, str += delim, while(-1 != (end = find(delim, str, beg)); result += { Sub(str, beg, end) }, beg = end + 1 ), result );