File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 388388assert len (a ) == 6
389389assert a .pop () == 100
390390
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 178178b"kok" .center (- 5 ) == b"kok"
179179
180180
181-
182181# ljust
183182assert [b"koki" .ljust (i , b"|" ) for i in range (3 , 10 )] == [
184183 b"koki" ,
577576assert b"42" .zfill (- 1 ) == b"42"
578577
579578# 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"
582581assert 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"
584583assert b"123456789123" .replace (b"23" , b"" ) == b"14567891"
585584
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 {
364364 . push ( x. as_bigint ( ) . byte_or ( vm) ?) ;
365365 Ok ( ( ) )
366366 }
367+
367368 #[ pymethod( name = "pop" ) ]
368369 fn pop ( self , vm : & VirtualMachine ) -> PyResult < u8 > {
369370 let bytes = & mut self . inner . borrow_mut ( ) . elements ;
370371 bytes
371372 . pop ( )
372373 . ok_or_else ( || vm. new_index_error ( "pop from empty bytearray" . to_string ( ) ) )
373374 }
375+
376+ #[ pymethod( name = "title" ) ]
377+ fn title ( self , vm : & VirtualMachine ) -> PyResult {
378+ Ok ( vm. ctx . new_bytearray ( self . inner . borrow ( ) . title ( ) ) )
379+ }
374380}
375381
376382// fn set_value(obj: &PyObjectRef, value: Vec<u8>) {
Original file line number Diff line number Diff line change @@ -975,6 +975,30 @@ impl PyByteInner {
975975
976976 Ok ( res)
977977 }
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+ }
9781002}
9791003
9801004pub 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 {
402402 ) -> PyResult {
403403 Ok ( vm. ctx . new_bytes ( self . inner . replace ( old, new, count) ?) )
404404 }
405+
406+ #[ pymethod( name = "title" ) ]
407+ fn title ( self , vm : & VirtualMachine ) -> PyResult {
408+ Ok ( vm. ctx . new_bytes ( self . inner . title ( ) ) )
409+ }
405410}
406411
407412#[ pyclass]
You can’t perform that action at this time.
0 commit comments