Skip to content

.toval dies without grace

Forums Forums SIMPOL Programming .toval dies without grace

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #24
    Jim Locker
    Member

    If a string containing non-numeric characters is passed to .toval, and nul is passed for the argument of characters to ignore, simpol crashes. I have implemented the following function to test the string and prevent the crash if the string is inappropriate. This function isn’t even close to perfect, but it is good enough for my purposes. // isnum takes a string and verifies that the only characters in the string are // zero through 9 or comma or period or minus sign. // // If this is true, it strips any commas out of the string (period is decimal place) // and then converts the string to a number. It then returns the number in nOurnum // and returns .true for the function. // // If the string contains any other characters, it returns 0 in iOurint and .false for // the function // function isnum(string sNumtext, number nOurnum) boolean bIsnum string sUsenum, sTstchar integer iNumlen, ii bIsnum = .true iNumlen = .len(sNumtext) sUsenum = “” ii = 1 while ii <= iNumlen sTstchar = .substr(sNumtext, ii, 1) if .like1(sTstchar, "[0-9]") or sTstchar == "," or sTstchar == "." or sTstchar == "-" if sTstchar != "," if sTstchar != "-" or(sTstchar == "-" and ii == 1) sUsenum = sUsenum + sTstchar end if end if ii = ii + 1 else bIsnum = .false ii = iNumlen + 1 end if end while if bIsnum nOurnum = .toval(sUsenum, .nul, 10) else nOurnum = 0 end if end function bIsnum

    #1690
    Michael
    Keymaster

    Jim wrote:
    > If a string containing non-numeric characters is passed to .toval,
    > and nul is passed for the argument of characters to ignore, simpol
    > crashes.
    >
    > I have implemented the following function to test the string and
    > prevent the crash if the string is inappropriate. This function
    > isn't even close to perfect, but it is good enough for my purposes.
    >

    Jim, rather than that, use nondigits() from the stringlib.sml (see
    project source for details):

    ///////////////////////////////////////////////////////////////////////////////
    // string nondigits()
    //
    // Parameters:
    // ===========
    // string s – The source string to examine.
    //
    // Return value:
    // =============
    // The string minus anything that is a digit in the range of 0-9.
    //
    // Description:
    // ============
    // This function is meant to be used to return a string that can be used
    // by .toval() in the ignorechars position to make sure that a value is
    // converted to an integer.
    ///////////////////////////////////////////////////////////////////////////////
    //
    function nondigits(string s)
    information "[simpol::return::string]" export
    end function s-"0"-"1"-"2"-"3"-"4"-"5"-"6"-"7"-"8"-"9"

    or this one:

    ///////////////////////////////////////////////////////////////////////////////
    // string nondigitsordecimal()
    //
    // Parameters:
    // ===========
    // string s – The source string to examine.
    //
    // Return value:
    // =============
    // The string minus anything that is a digit in the range of 0-9, the
    // decimal point and the square brackets used for repeating decimals.
    // This does not handle localized decimal characters.
    //
    // Description:
    // ============
    // This function is meant to be used to return a string that can be used
    // by .toval() in the ignorechars position to make sure that a value is
    // converted to a number or integer depending on input.
    ///////////////////////////////////////////////////////////////////////////////
    //
    function nondigitsordecimal(string s)
    information "[simpol::return::string]" export
    end function nondigits(s) – "." – "[" – "]"

    Ciao, Neil

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.