@@ -626,15 +626,44 @@ class Ocl
626626}
627627
628628 static func indexOf( str: String , ch: String ) -> Int
629- { var count = 0
630- for index in str. indices
631- { let c : Character = str [ index]
632- count = count + 1
633- if ( String ( c) == ch)
634- { return count }
635- }
636- return 0
637- }
629+ { var res : Int = 0
630+ var found : Bool = false
631+ if ch. count == 0
632+ { return 0 }
633+
634+ let sepchars = chars ( str: ch)
635+ var ind : Int = 0
636+
637+ for index in str. indices
638+ { let c : Character = str [ index]
639+
640+ if found && ( ind < sepchars. count)
641+ { if ( c == sepchars [ ind] )
642+ { ind = ind + 1 }
643+ else
644+ { found = false
645+ res = res + ind
646+ ind = 0
647+ }
648+ }
649+ else if found
650+ { return res + 1 }
651+
652+ if found == false
653+ { if c == sepchars [ 0 ]
654+ { found = true
655+ ind = 1
656+ }
657+ }
658+
659+ if found == false
660+ { res = res + 1 }
661+ }
662+
663+ if found
664+ { return res + 1 }
665+ return 0
666+ }
638667
639668 static func stringSubrange( str : String , st : Int , en : Int ) -> String
640669 { var result : [ Character ] = [ Character] ( )
@@ -687,7 +716,14 @@ class Ocl
687716 return result
688717 }
689718
690- static func characters( str: String ) -> [ Character ]
719+ static func characters( str: String ) -> [ String ]
720+ { var res : [ String ] = [ String] ( )
721+ for ind in str. indices
722+ { res. append ( String ( str [ ind] ) ) }
723+ return res
724+ }
725+
726+ static func chars( str: String ) -> [ Character ]
691727 { var res : [ Character ] = [ Character] ( )
692728 for ind in str. indices
693729 { res. append ( str [ ind] ) }
@@ -696,14 +732,14 @@ class Ocl
696732
697733 static func insertAtString( s1 : String , s2 : String , ind : Int ) -> String
698734 { var result : [ Character ] = [ Character] ( )
699- let seq1 = Ocl . characters ( str: s1)
700- let seq2 = Ocl . characters ( str: s2)
735+ let seq1 = Ocl . chars ( str: s1)
736+ let seq2 = Ocl . chars ( str: s2)
701737 result = Ocl . insertAt ( s1: seq1, s2: seq2, ind: ind)
702738 return String ( result)
703739 }
704740
705- static func reverseString( s : String ) -> String
706- { let result : [ Character ] = Ocl . characters ( str: s )
741+ static func reverseString( str : String ) -> String
742+ { let result : [ Character ] = Ocl . chars ( str: str )
707743 let rev = Ocl . reverse ( s: result)
708744 return String ( rev)
709745 }
@@ -725,6 +761,31 @@ class Ocl
725761 return result
726762 }
727763
764+ static func before( str: String , sep: String ) -> String
765+ { let ind = indexOf ( str: str, ch: sep)
766+ if ind > 0
767+ { return stringSubrange ( str: str, st: 1 , en: ind- 1 ) }
768+ return str
769+ } // Single-character sep only
770+
771+ static func after( str: String , sep: String ) -> String
772+ { let revstr = reverseString ( str: str)
773+ let revsep = reverseString ( str: sep)
774+ let ind = indexOf ( str: revstr, ch: revsep)
775+ if ind > 0
776+ { let res = stringSubrange ( str: revstr, st: 1 , en: ind- 1 )
777+ return reverseString ( str: res)
778+ }
779+ return " "
780+ } // Single-character sep only
781+
782+ static func matches( str: String , pattern: String ) -> Bool
783+ { let rge = NSRange ( location: 0 , length: str. utf16. count)
784+ var regexp = try ! NSRegularExpression ( pattern: pattern)
785+ let pred = regexp. firstMatch ( in: str, options: [ ] , range: rge)
786+ return pred != nil
787+ }
788+
728789 /* Only for Swift 5+
729790 static func before(str: String, sep: String) -> String
730791 { if let ind = str.firstIndex(of: sep)
0 commit comments