Skip to content

Commit 08aaee5

Browse files
committed
Create integerparsing init for substring
When parsing a substring, _parseASCIISlowPath is being executed. Higher performance can be expected just by wrapping it with a String once.
1 parent 940a155 commit 08aaee5

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

stdlib/public/core/IntegerParsing.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,31 @@ extension FixedWidthInteger {
197197
public init?(_ description: String) {
198198
self.init(description, radix: 10)
199199
}
200+
201+
/// Creates a new integer value from the given Substring.
202+
///
203+
/// Substring can be parsed at a high speed just by substituting strings.
204+
///
205+
/// The Substring passed as `description` may begin with a plus or minus sign
206+
/// character (`+` or `-`), followed by one or more numeric digits (`0-9`).
207+
///
208+
/// let x = Int("123")
209+
/// // x == 123
210+
///
211+
/// If `description` is in an invalid format, or if the value it denotes in
212+
/// base 10 is not representable, the result is `nil`. For example, the
213+
/// following conversions result in `nil`:
214+
///
215+
/// Int(" 100") // Includes whitespace
216+
/// Int("21-50") // Invalid format
217+
/// Int("ff6600") // Characters out of bounds
218+
/// Int("10000000000000000000000000") // Out of range
219+
///
220+
/// - Parameter description: The ASCII representation of a number.
221+
@inlinable
222+
@_semantics("optimize.sil.specialize.generic.partial.never")
223+
@inline(__always)
224+
public init?(_ description: Substring) {
225+
self.init(String(description), radix: 10)
226+
}
200227
}

0 commit comments

Comments
 (0)