@@ -206,6 +206,18 @@ replaced.`)
206206 return Bool (false ), nil
207207 }, 0 , "startswith(prefix[, start[, end]]) -> bool" )
208208
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+
209221}
210222
211223// Type of this object
@@ -679,6 +691,54 @@ func (s String) Replace(args Tuple) (Object, error) {
679691 return String (strings .Replace (string (s ), old , new , cnt )), nil
680692}
681693
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+
682742// Check stringerface is satisfied
683743var (
684744 _ richComparison = String ("" )
0 commit comments