How do you raise the error 'Insufficient string capacity'?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How do you raise the error 'Insufficient string capacity'?

tungsten
The OS error code listing includes

19 - String.setInt - Insufficient string capacity

How would you raise this error in the code for String.setInt? Specifically, what check could you do within the function that raises this error.

The simplest check I can think of is whether the string's max length is greater or equal to 5 (and thus has enough space to represent the maximum number of digits possible on the Hack computer). However this falls short for integers less than 5 characters in length which can be represented with smaller strings.

At the moment, I'm just letting the following error handle the overflow scenario...

17 - String.appendChar - String is full
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How do you raise the error 'Insufficient string capacity'?

cadet1620
Administrator
Since setInt() is a String method, it can check the current and maximum length fields and prevent appendChar() from failing.

My setInt() main loop is basically
    while (len < max) {
        ...
        append next character
        if conversion complete
            return;
        ...
    }
    do Sys.error(19); 
--Mark
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How do you raise the error 'Insufficient string capacity'?

tungsten
I see. Thanks!
Loading...