String Extra Functions
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © kaigouthro
//@version=5

// @description Additional String shortcuts
library("string_extras")

// @function    last char of a string
// @param       _string String to process
// @returns     last char of a string
export last    (string _string                                     ) =>
    _l = str.length(_string), _l > 1 ? str.substring(_string,_l-1,_l) : ''

// @function    string without first char
// @param       _string String to process
// @returns     string without first char
export shift   (string _string                                     ) =>
    str.length(_string) > 1 ? str.substring(_string,1) : ''

// @function    string without last char
// @param       _string String to process
// @returns     string without last char
export pop     (string _string                                     ) =>
    _l = str.length(_string),_l>1?str.substring(_string,0,_l-1):_string

// @function    get specific char of a string
// @param       _string String to process
// @param       _position int
// @returns     string _string
export get     (string _string, int _position                      ) =>
    _position >=0 and _position < str.length(_string) ?
      str.substring(_string,_position-1,_position) : _string

// @function    push to end of a string
// @param       _string String to process
// @param       _char string
// @returns     string _string
export push    (string _string, string _char                       ) =>
    _string + _char

// @function    unshift char to prepend string
// @param       _string String to process
// @param       _char string
// @returns     string _string
export unshift (string _string, string _char                       ) =>
    _char + _string 

// @function    string from first char until input, if it exists, or itself, with option to cut off char
// @param       _string String to process
// @param       _char string
// @returns     string up until/including the dsired character 
export head    (string _string, string _char, bool _include = false) =>
    str.match(_string,'\[\^'+_char+'\]\+') + (_include ? _char : '' )   

// @function    string from last input char until end, if it exists, or itself with option to cut off char
// @param       _string String to process
// @param       _char string
// @returns     string up until/including the dsired character
export tail    (string _string, string _char, bool _include = false) =>
    (_include ? _char : '' ) + str.match(_string,_char+'\(\?=\[\^'+_char+'\]\*\$\)\(\.\+\\n\*\)\+')

// @function    returns the _occurabnce (1st, 2nd..) index of a character in a string if set , or last
// @param       _string String to process
// @param       _char string
// @returns     int index of character
export index     (string _string, string _char , int _occurance = na) =>
    int _idx = na
    occurance = 0
    for i = 0 to str.length(_string)
        _s = str.substring(_string, i, i+1)
        if _s == _char
            occurance += 1 
            if occurance == _occurance
                break
                _idx := i
            else
                _idx := i
