Files
swift-mirror/stdlib/public/core/FloatingPointParsing.swift.gyb
Dmitri Hrybenko dd3194a18c stdlib: adopt @warn_unused_result
rdar://20957486

Swift SVN r31048
2015-08-06 14:53:18 +00:00

83 lines
2.2 KiB
Plaintext

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import SwiftShims
%{
allFloatBits = [32, 64, 80]
def floatName(bits):
if bits == 32:
return 'Float'
if bits == 64:
return 'Double'
if bits == 80:
return 'Float80'
cFuncSuffix2 = {32: 'f', 64: 'd', 80: 'ld'}
}%
/// Return true iff isspace(u) would return nonzero when the current
/// locale is the C locale.
@warn_unused_result
internal func _isspace_clocale(u: UTF16.CodeUnit) -> Bool {
return "\t\n\u{b}\u{c}\r ".utf16.contains(u)
}
% for bits in allFloatBits:
% Self = floatName(bits)
% if bits == 80:
#if arch(i386) || arch(x86_64)
% end
//===--- Parsing ----------------------------------------------------------===//
extension ${Self} {
/// Construct from an ASCII representation.
///
/// Returns the result of calling the POSIX function
/// `strto${cFuncSuffix2[bits]}_l` using the "C" locale, unless
/// `text` contains non-ASCII text or whitespace, or is not
/// completely consumed by the call. Otherwise, returns `nil`.
///
/// See the `strto${cFuncSuffix2[bits]} (3)` man page for details of
/// the exact format accepted.
public init?(_ text: String) {
let u16 = text.utf16
func parseNTBS(chars: UnsafePointer<CChar>) -> (${Self}, Int) {
var result: ${Self} = 0
let endPtr = withUnsafeMutablePointer(&result) {
_swift_stdlib_strto${cFuncSuffix2[bits]}_clocale(
chars, UnsafeMutablePointer($0))
}
return (result, endPtr == nil ? 0 : UnsafePointer(endPtr) - chars)
}
let (result, n) = text.withCString(parseNTBS)
if n == 0 || n != u16.count
|| u16.contains({ $0 > 127 || _isspace_clocale($0) }) {
return nil
}
self = result
}
}
% if bits == 80:
#endif
% end
% end