diff --git a/ngcp/utils.lua b/ngcp/utils.lua index 3bfcd0c..c5f188f 100644 --- a/ngcp/utils.lua +++ b/ngcp/utils.lua @@ -256,6 +256,22 @@ function utils.explode(delimiter, text) return list end +-- from string to table with all the values of that string from 1 to len +-- "123" -> {'1', '12', '123'} +function us.explode_values(str) + local list = {} + local len + + if not str then + error("string is nil") + end + len = string.len(str) + for i=1,len do + table.insert(list, string.sub(str, 1, i)) + end + return list +end + function us.starts(String,Start) return string.sub(String,1,string.len(Start))==Start end diff --git a/tests/utils.lua b/tests/utils.lua index 95b97f5..fbee6d7 100644 --- a/tests/utils.lua +++ b/tests/utils.lua @@ -161,6 +161,13 @@ TestUtils = {} assertItemsEquals(utils.explode('=>',"1=>2=>3"), {'1','2','3'}) end + function TestUtils:test_string_explode_values() + assertError(utils.string.explode_values, nil) + assertItemsEquals(utils.string.explode_values(''), {}) + assertItemsEquals(utils.string.explode_values('1'), {'1'}) + assertItemsEquals(utils.string.explode_values('123'), {'1','12','123'}) + end + function TestUtils:test_starts() assertError(utils.string.stats, nil, "g") assertTrue(utils.string.starts("goga", "g"))