@@ -84,16 +84,22 @@ extension String {
84
84
return self
85
85
}
86
86
87
- func stripLeadingZeroes( ) -> String ? {
88
- let hex = self . addHexPrefix ( )
89
- guard let matcher = try ? NSRegularExpression ( pattern: " ^(?<prefix>0x)0*(?<end>[0-9a-fA-F]*)$ " , options: NSRegularExpression . Options. dotMatchesLineSeparators) else { return nil }
90
- let match = matcher. captureGroups ( string: hex, options: NSRegularExpression . MatchingOptions. anchored)
91
- guard let prefix = match [ " prefix " ] else { return nil }
92
- guard let end = match [ " end " ] else { return nil }
93
- if ( end != " " ) {
94
- return prefix + end
87
+ /// Strips leading zeroes from a HEX string.
88
+ /// ONLY HEX string format is supported.
89
+ /// - Returns: string with stripped leading zeroes (and 0x prefix) or unchaged string.
90
+ func stripLeadingZeroes( ) -> String {
91
+ let hex = addHexPrefix ( )
92
+ guard let matcher = try ? NSRegularExpression ( pattern: " ^(?<prefix>0x)(?<leadingZeroes>0+)(?<end>[0-9a-fA-F]*)$ " ,
93
+ options: . dotMatchesLineSeparators)
94
+ else {
95
+ NSLog ( " stripLeadingZeroes(): failed to parse regex pattern. " )
96
+ return self
95
97
}
96
- return " 0x0 "
98
+ let match = matcher. captureGroups ( string: hex, options: . anchored)
99
+ guard match [ " leadingZeroes " ] != nil ,
100
+ let prefix = match [ " prefix " ] ,
101
+ let end = match [ " end " ] else { return self }
102
+ return end != " " ? prefix + end : " 0x0 "
97
103
}
98
104
99
105
func matchingStrings( regex: String ) -> [ [ String ] ] {
0 commit comments