functionStringToMatrix
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RicardoSantos

//@version=5

// @description Provides unbound methods (no error checking) to parse a string into a float or int matrix.
library(title='functionStringToMatrix')

import RicardoSantos/DebugConsole/10 as console
[_T, _C] = console.init()


// @function Parse a string into a float matrix.
// @param str          , string, the formated string to parse.
// @param interval_sep , string, cell interval separator token.
// @param start_tk     , string, row start token.
// @param end_tk       , string, row end token.
// @returns matrix<float>, parsed float matrix.
export to_matrix_float (
  string str, 
  string interval_sep=',', 
  string start_tk='[', 
  string end_tk=']'
  ) => //{
    //
	// remove spaces, new lines and block initializing tokens
	string _cleaned = str.replace_all(str.replace_all(str.replace_all(str, start_tk, ''), '\n', ''), ' ', '')
	array<string> _str_rows = str.split(_cleaned, end_tk)
	array.pop(_str_rows)//remove the end token
	int _size_cols = array.size(str.split(array.get(_str_rows, 0), interval_sep))
    matrix<float> _M = matrix.new<float>(array.size(_str_rows), _size_cols)
    for [_i, _row] in _str_rows
        array<string> _s = str.split(_row, interval_sep)
        for [_j, _e] in _s
            matrix.set(_M, _i, _j, str.tonumber(_e))
    _M
//}

// @function Parse a string into a int matrix.
// @param str          , string, the formated string to parse.
// @param interval_sep , string, cell interval separator token.
// @param start_tk     , string, row start token.
// @param end_tk       , string, row end token.
// @returns matrix<int>, parsed int matrix.
export to_matrix_int (
  string str, 
  string interval_sep=',', 
  string start_tk='[', 
  string end_tk=']'
  ) => //{
    //
	// remove spaces, new lines and block initializing tokens
	string _cleaned = str.replace_all(str.replace_all(str.replace_all(str, start_tk, ''), '\n', ''), ' ', '')
	array<string> _str_rows = str.split(_cleaned, end_tk)
	array.pop(_str_rows)//remove the end token
	int _size_cols = array.size(str.split(array.get(_str_rows, 0), interval_sep))
    matrix<int> _M = matrix.new<int>(array.size(_str_rows), _size_cols)
    for [_i, _row] in _str_rows
        array<string> _s = str.split(_row, interval_sep)
        for [_j, _e] in _s
            matrix.set(_M, _i, _j, int(str.tonumber(_e)))
    _M
//}

// @function Parse a string into a string matrix.
// @param str          , string, the formated string to parse.
// @param interval_sep , string, cell interval separator token.
// @param start_tk     , string, row start token.
// @param end_tk       , string, row end token.
// @returns matrix<string>, parsed string matrix.
export to_matrix_string (
  string str, 
  string interval_sep=',', 
  string start_tk='[', 
  string end_tk=']'
  ) => //{
    //
	// remove spaces, new lines and block initializing tokens
	string _cleaned = str.replace_all(str.replace_all(str.replace_all(str, start_tk, ''), '\n', ''), ' ', '')
	array<string> _str_rows = str.split(_cleaned, end_tk)
	array.pop(_str_rows)//remove the end token
	int _size_cols = array.size(str.split(array.get(_str_rows, 0), interval_sep))
    matrix<string> _M = matrix.new<string>(array.size(_str_rows), _size_cols)
    for [_i, _row] in _str_rows
        array<string> _s = str.split(_row, interval_sep)
        for [_j, _e] in _s
            matrix.set(_M, _i, _j, _e)
    _M
//}

string s0 = input.text_area('[[-0,1,2.1][-0,1,2.2][-0,1,2.3]]')
string s1 = input.text_area('-0,1,2.1 | -0,1,2.2 | -0,1,2.3|')
string s2 = input.text_area('-0,1,2.1;-0,1,2.2;-0,1,2.3;')
string s3 = input.text_area('((-0,1,2.1)(-0,1,2.2)(-0,1,2.3))')
string s4 = input.text_area('[[a,b,c.d][a,b,c.d][a,b,c.d]]')

console.queue_one(_C, str.format('"{0}":\n{1}', s0, str.tostring(to_matrix_float(s0))))
console.queue_one(_C, str.format('"{0}":\n{1}', s1, str.tostring(to_matrix_float(s1, ',', '', '|'))))
console.queue_one(_C, str.format('"{0}":\n{1}', s2, str.tostring(to_matrix_float(s2, ',', '', ';'))))
console.queue_one(_C, str.format('"{0}":\n{1}', s3, str.tostring(to_matrix_int(s3, ',', '(', ')'))))
console.queue_one(_C, str.format('"{0}":\n{1}', s4, str.tostring(to_matrix_string(s4))))

console.update(_T, _C)