File tree 5 files changed +51
-5
lines changed 5 files changed +51
-5
lines changed Original file line number Diff line number Diff line change 388
388
assert len (a ) == 6
389
389
assert a .pop () == 100
390
390
391
- import bytes as bbytes
391
+ # title
392
+ assert bytearray (b"Hello world" ).title () == bytearray (b"Hello World" )
393
+ assert (
394
+ bytearray (b"they're bill's friends from the UK" ).title ()
395
+ == bytearray (b"They'Re Bill'S Friends From The Uk" )
396
+ )
397
+
Original file line number Diff line number Diff line change 178
178
b"kok" .center (- 5 ) == b"kok"
179
179
180
180
181
-
182
181
# ljust
183
182
assert [b"koki" .ljust (i , b"|" ) for i in range (3 , 10 )] == [
184
183
b"koki" ,
577
576
assert b"42" .zfill (- 1 ) == b"42"
578
577
579
578
# replace
580
- assert b"123456789123" .replace (b"23" , b"XX" ) == b' 1XX4567891XX'
581
- assert b"123456789123" .replace (b"23" , b"XX" , 1 ) == b' 1XX456789123'
579
+ assert b"123456789123" .replace (b"23" , b"XX" ) == b" 1XX4567891XX"
580
+ assert b"123456789123" .replace (b"23" , b"XX" , 1 ) == b" 1XX456789123"
582
581
assert b"123456789123" .replace (b"23" , b"XX" , 0 ) == b"123456789123"
583
- assert b"123456789123" .replace (b"23" , b"XX" , - 1 ) == b' 1XX4567891XX'
582
+ assert b"123456789123" .replace (b"23" , b"XX" , - 1 ) == b" 1XX4567891XX"
584
583
assert b"123456789123" .replace (b"23" , b"" ) == b"14567891"
585
584
585
+ # title
586
+ assert b"Hello world" .title () == b"Hello World"
587
+ assert (
588
+ b"they're bill's friends from the UK" .title ()
589
+ == b"They'Re Bill'S Friends From The Uk"
590
+ )
Original file line number Diff line number Diff line change @@ -364,13 +364,19 @@ impl PyByteArrayRef {
364
364
. push ( x. as_bigint ( ) . byte_or ( vm) ?) ;
365
365
Ok ( ( ) )
366
366
}
367
+
367
368
#[ pymethod( name = "pop" ) ]
368
369
fn pop ( self , vm : & VirtualMachine ) -> PyResult < u8 > {
369
370
let bytes = & mut self . inner . borrow_mut ( ) . elements ;
370
371
bytes
371
372
. pop ( )
372
373
. ok_or_else ( || vm. new_index_error ( "pop from empty bytearray" . to_string ( ) ) )
373
374
}
375
+
376
+ #[ pymethod( name = "title" ) ]
377
+ fn title ( self , vm : & VirtualMachine ) -> PyResult {
378
+ Ok ( vm. ctx . new_bytearray ( self . inner . borrow ( ) . title ( ) ) )
379
+ }
374
380
}
375
381
376
382
// fn set_value(obj: &PyObjectRef, value: Vec<u8>) {
Original file line number Diff line number Diff line change @@ -975,6 +975,30 @@ impl PyByteInner {
975
975
976
976
Ok ( res)
977
977
}
978
+
979
+ pub fn title ( & self ) -> Vec < u8 > {
980
+ let mut res = vec ! [ ] ;
981
+ let mut spaced = true ;
982
+
983
+ for i in self . elements . iter ( ) {
984
+ match i {
985
+ 65 ..=90 | 97 ..=122 => {
986
+ if spaced {
987
+ res. push ( i. to_ascii_uppercase ( ) ) ;
988
+ spaced = false
989
+ } else {
990
+ res. push ( i. to_ascii_lowercase ( ) ) ;
991
+ }
992
+ }
993
+ _ => {
994
+ res. push ( * i) ;
995
+ spaced = true
996
+ }
997
+ }
998
+ }
999
+
1000
+ res
1001
+ }
978
1002
}
979
1003
980
1004
pub fn try_as_byte ( obj : & PyObjectRef ) -> Option < Vec < u8 > > {
Original file line number Diff line number Diff line change @@ -402,6 +402,11 @@ impl PyBytesRef {
402
402
) -> PyResult {
403
403
Ok ( vm. ctx . new_bytes ( self . inner . replace ( old, new, count) ?) )
404
404
}
405
+
406
+ #[ pymethod( name = "title" ) ]
407
+ fn title ( self , vm : & VirtualMachine ) -> PyResult {
408
+ Ok ( vm. ctx . new_bytes ( self . inner . title ( ) ) )
409
+ }
405
410
}
406
411
407
412
#[ pyclass]
You can’t perform that action at this time.
0 commit comments