@@ -206,6 +206,18 @@ replaced.`)
206
206
return Bool (false ), nil
207
207
}, 0 , "startswith(prefix[, start[, end]]) -> bool" )
208
208
209
+ StringType .Dict ["strip" ] = MustNewMethod ("strip" , func (self Object , args Tuple , kwargs StringDict ) (Object , error ) {
210
+ return self .(String ).Strip (args )
211
+ }, 0 , "strip(chars) -> replace chars from begining and end of string" )
212
+
213
+ StringType .Dict ["rstrip" ] = MustNewMethod ("rstrip" , func (self Object , args Tuple , kwargs StringDict ) (Object , error ) {
214
+ return self .(String ).RStrip (args )
215
+ }, 0 , "rstrip(chars) -> replace chars from end of string" )
216
+
217
+ StringType .Dict ["lstrip" ] = MustNewMethod ("lstrip" , func (self Object , args Tuple , kwargs StringDict ) (Object , error ) {
218
+ return self .(String ).LStrip (args )
219
+ }, 0 , "lstrip(chars) -> replace chars from begining of string" )
220
+
209
221
}
210
222
211
223
// Type of this object
@@ -679,6 +691,54 @@ func (s String) Replace(args Tuple) (Object, error) {
679
691
return String (strings .Replace (string (s ), old , new , cnt )), nil
680
692
}
681
693
694
+ func stripFunc (args Tuple ) (func (rune ) bool , error ) {
695
+ var (
696
+ pyval Object = None
697
+ )
698
+ err := ParseTuple (args , "|s" , & pyval )
699
+ if err != nil {
700
+ return nil , err
701
+ }
702
+ f := unicode .IsSpace
703
+ switch v := pyval .(type ) {
704
+ case String :
705
+ chars := []rune (string (v ))
706
+ f = func (s rune ) bool {
707
+ for _ , i := range chars {
708
+ if s == i {
709
+ return true
710
+ }
711
+ }
712
+ return false
713
+ }
714
+ }
715
+ return f , nil
716
+ }
717
+
718
+ func (s String ) Strip (args Tuple ) (Object , error ) {
719
+ f , err := stripFunc (args )
720
+ if err != nil {
721
+ return nil , err
722
+ }
723
+ return String (strings .TrimFunc (string (s ), f )), nil
724
+ }
725
+
726
+ func (s String ) LStrip (args Tuple ) (Object , error ) {
727
+ f , err := stripFunc (args )
728
+ if err != nil {
729
+ return nil , err
730
+ }
731
+ return String (strings .TrimLeftFunc (string (s ), f )), nil
732
+ }
733
+
734
+ func (s String ) RStrip (args Tuple ) (Object , error ) {
735
+ f , err := stripFunc (args )
736
+ if err != nil {
737
+ return nil , err
738
+ }
739
+ return String (strings .TrimRightFunc (string (s ), f )), nil
740
+ }
741
+
682
742
// Check stringerface is satisfied
683
743
var (
684
744
_ richComparison = String ("" )
0 commit comments