mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
Check the size of fillchar passed to str methods (ljust, rjust) (#236)
* check the size of fillchar passed to str methods (ljust, rjust) * count characters using u8_length instead of size
This commit is contained in:
parent
cb15db1f0e
commit
b1115a4c8f
@ -734,6 +734,7 @@ void init_builtins(VM* _vm) {
|
|||||||
int delta = width - self.u8_length();
|
int delta = width - self.u8_length();
|
||||||
if(delta <= 0) return args[0];
|
if(delta <= 0) return args[0];
|
||||||
const Str& fillchar = CAST(Str&, args[2]);
|
const Str& fillchar = CAST(Str&, args[2]);
|
||||||
|
if (fillchar.u8_length() != 1) vm->TypeError("The fill character must be exactly one character long");
|
||||||
SStream ss;
|
SStream ss;
|
||||||
ss << self;
|
ss << self;
|
||||||
for(int i=0; i<delta; i++) ss << fillchar;
|
for(int i=0; i<delta; i++) ss << fillchar;
|
||||||
@ -747,6 +748,7 @@ void init_builtins(VM* _vm) {
|
|||||||
int delta = width - self.u8_length();
|
int delta = width - self.u8_length();
|
||||||
if(delta <= 0) return args[0];
|
if(delta <= 0) return args[0];
|
||||||
const Str& fillchar = CAST(Str&, args[2]);
|
const Str& fillchar = CAST(Str&, args[2]);
|
||||||
|
if (fillchar.u8_length() != 1) vm->TypeError("The fill character must be exactly one character long");
|
||||||
SStream ss;
|
SStream ss;
|
||||||
for(int i=0; i<delta; i++) ss << fillchar;
|
for(int i=0; i<delta; i++) ss << fillchar;
|
||||||
ss << self;
|
ss << self;
|
||||||
|
@ -157,8 +157,18 @@ assert b[5:2:-2] == [',', 'l']
|
|||||||
a = '123'
|
a = '123'
|
||||||
assert a.rjust(5) == ' 123'
|
assert a.rjust(5) == ' 123'
|
||||||
assert a.rjust(5, '0') == '00123'
|
assert a.rjust(5, '0') == '00123'
|
||||||
|
try:
|
||||||
|
a.rjust(5, '00')
|
||||||
|
exit(1)
|
||||||
|
except TypeError:
|
||||||
|
pass
|
||||||
assert a.ljust(5) == '123 '
|
assert a.ljust(5) == '123 '
|
||||||
assert a.ljust(5, '0') == '12300'
|
assert a.ljust(5, '0') == '12300'
|
||||||
|
try:
|
||||||
|
a.ljust(5, '00')
|
||||||
|
exit(1)
|
||||||
|
except TypeError:
|
||||||
|
pass
|
||||||
|
|
||||||
assert '\x30\x31\x32' == '012'
|
assert '\x30\x31\x32' == '012'
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user