Fix bug in LEFT$ MID$ and RIGHT$ (#45)

"The message ?ILLEGAL QUANTITY ERROR is displayed
 if \aexpr\<1 or \aexpr\>255.

Co-authored-by: Colin Leroy-Mira <colin.leroy-mira@sigfox.com>
This commit is contained in:
Colin Leroy-Mira 2022-12-06 03:46:20 +01:00 committed by GitHub
parent 252b39dc08
commit 3d37fc357a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 3 deletions

View File

@ -1131,9 +1131,27 @@ this.basic = (function() {
return state.prng.last;
}, 'number', 'number');
funlib[kws.LEFT$] = funcsign(function LEFT$(s, n) { return s.substring(0, n); }, 'string', 'string', 'number');
funlib[kws.MID$] = funcsign(function MID$(s, n, n2) { return n2 === (void 0) ? s.substring(n - 1) : s.substring(n - 1, n + n2 - 1); }, 'string', 'string', 'number', 'number?');
funlib[kws.RIGHT$] = funcsign(function RIGHT$(s, n) { return s.length < n ? s : s.substring(s.length - n); }, 'string', 'string', 'number');
funlib[kws.LEFT$] = funcsign(function LEFT$(s, n) {
if (n < 1 || n > 255) {
runtime_error(ERRORS.ILLEGAL_QUANTITY);
}
return s.substring(0, n);
}, 'string', 'string', 'number');
funlib[kws.MID$] = funcsign(function MID$(s, n, n2) {
if (n < 1 || n > 255) {
runtime_error(ERRORS.ILLEGAL_QUANTITY);
}
if (n2 < 1 || n2 > 255) {
runtime_error(ERRORS.ILLEGAL_QUANTITY);
}
return n2 === (void 0) ? s.substring(n - 1) : s.substring(n - 1, n + n2 - 1);
}, 'string', 'string', 'number', 'number?');
funlib[kws.RIGHT$] = funcsign(function RIGHT$(s, n) {
if (n < 1 || n > 255) {
runtime_error(ERRORS.ILLEGAL_QUANTITY);
}
return s.length < n ? s : s.substring(s.length - n);
}, 'string', 'string', 'number');
funlib[kws.POS] = funcsign(function POS(n) { return env.tty.getCursorPosition().x; }, 'number', 'number');
funlib[kws.SCRN] = funcsign(function SCRN(x, y) {