1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
|
//===--------------------------- OSLogFloatFormatting.swift ------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===------------------------------------------------------------------------------===//
// This file defines types and functions for specifying formatting of
// floating-point typed interpolations passed to the os log APIs.
@frozen
public struct OSLogFloatFormatting {
/// When set, a `+` will be printed for all non-negative floats.
@usableFromInline
internal var explicitPositiveSign: Bool
/// Whether to use uppercase letters to represent numerals greater than 9
/// (default is to use lowercase). This applies to hexadecimal digits, NaN, Inf,
/// the symbols E and X used to denote exponent and hex format.
@usableFromInline
internal var uppercase: Bool
// Note: includePrefix is not supported for FloatFormatting. The format specifier %a
// always prints a prefix, %efg don't need one.
/// Number of digits to display following the radix point. Hex notation does not accept
/// a precision. For non-hex notations, precision can be a dynamic value. The default
/// precision is 6 for non-hex notations.
@usableFromInline
internal var precision: (() -> Int)?
@usableFromInline
internal enum Notation {
/// Hexadecimal formatting.
case hex
/// fprintf's `%f` formatting.
///
/// Prints all digits before the radix point, and `precision` digits following
/// the radix point. If `precision` is zero, the radix point is omitted.
///
/// Note that very large floating-point values may print quite a lot of digits
/// when using this format, even if `precision` is zero--up to hundreds for
/// `Double`, and thousands for `Float80`. Note also that this format is
/// very likely to print non-zero values as all-zero. If these are a concern, use
/// `.exponential` or `.hybrid` instead.
///
/// Systems may impose an upper bound on the number of digits that are
/// supported following the radix point.
case fixed
/// fprintf's `%e` formatting.
///
/// Prints the number in the form [-]d.ddd...dde±dd, with `precision` significant
/// digits following the radix point. Systems may impose an upper bound on the number
/// of digits that are supported.
case exponential
/// fprintf's `%g` formatting.
///
/// Behaves like `.fixed` when the number is scaled close to 1.0, and like
/// `.exponential` if it has a very large or small exponent.
case hybrid
}
@usableFromInline
internal var notation: Notation
@_transparent
@usableFromInline
internal init(
explicitPositiveSign: Bool = false,
uppercase: Bool = false,
precision: (() -> Int)?,
notation: Notation
) {
self.explicitPositiveSign = explicitPositiveSign
self.uppercase = uppercase
self.precision = precision
self.notation = notation
}
/// Displays an interpolated floating-point value in fprintf's `%f` format with
/// default precision.
///
/// Prints all digits before the radix point, and 6 digits following the radix point.
/// Note also that this format is very likely to print non-zero values as all-zero.
///
/// Note that very large floating-point values may print quite a lot of digits
/// when using this format --up to hundreds for `Double`. Note also that this
/// format is very likely to print non-zero values as all-zero. If these are a concern,
/// use `.exponential` or `.hybrid` instead.
@_semantics("constant_evaluable")
@inlinable
@_optimize(none)
public static var fixed: OSLogFloatFormatting { .fixed() }
/// Displays an interpolated floating-point value in fprintf's `%f` format with
/// specified precision, and optional sign and case.
///
/// Prints all digits before the radix point, and `precision` digits following
/// the radix point. If `precision` is zero, the radix point is omitted.
///
/// Note that very large floating-point values may print quite a lot of digits
/// when using this format, even if `precision` is zero--up to hundreds for
/// `Double`. Note also that this format is very likely to print non-zero values as
/// all-zero. If these are a concern, use `.exponential` or `.hybrid` instead.
///
/// Systems may impose an upper bound on the number of digits that are
/// supported following the radix point.
///
/// All parameters to this function except `precision` must be boolean literals.
///
/// - Parameters:
/// - precision: Number of digits to display after the radix point.
/// - explicitPositiveSign: Pass `true` to add a + sign to non-negative
/// numbers.
/// - uppercase: Pass `true` to use uppercase letters or `false` to use
/// lowercase letters. The default is `false`.
@_semantics("constant_evaluable")
@inlinable
@_optimize(none)
public static func fixed(
precision: @escaping @autoclosure () -> Int,
explicitPositiveSign: Bool = false,
uppercase: Bool = false
) -> OSLogFloatFormatting {
return OSLogFloatFormatting(
explicitPositiveSign: explicitPositiveSign,
uppercase: uppercase,
precision: precision,
notation: .fixed
)
}
/// Displays an interpolated floating-point value in fprintf's `%f` format with
/// default precision, and optional sign and case.
///
/// Prints all digits before the radix point, and 6 digits following the radix point.
/// Note also that this format is very likely to print non-zero values as all-zero.
///
/// Note that very large floating-point values may print quite a lot of digits
/// when using this format, even if `precision` is zero--up to hundreds for
/// `Double`. Note also that this format is very likely to print non-zero values as
/// all-zero. If these are a concern, use `.exponential` or `.hybrid` instead.
///
/// Systems may impose an upper bound on the number of digits that are
/// supported following the radix point.
///
/// All parameters to this function must be boolean literals.
/// - Parameters:
/// - explicitPositiveSign: Pass `true` to add a + sign to non-negative
/// numbers.
/// - uppercase: Pass `true` to use uppercase letters or `false` to use
/// lowercase letters. The default is `false`.
@_semantics("constant_evaluable")
@inlinable
@_optimize(none)
public static func fixed(
explicitPositiveSign: Bool = false,
uppercase: Bool = false
) -> OSLogFloatFormatting {
return OSLogFloatFormatting(
explicitPositiveSign: explicitPositiveSign,
uppercase: uppercase,
precision: nil,
notation: .fixed
)
}
/// Displays an interpolated floating-point value in hexadecimal format.
@_semantics("constant_evaluable")
@inlinable
@_optimize(none)
public static var hex: OSLogFloatFormatting { .hex() }
/// Displays an interpolated floating-point value in hexadecimal format with
/// optional sign and case.
///
/// All parameters to this function must be boolean literals.
///
/// - Parameters:
/// - explicitPositiveSign: Pass `true` to add a + sign to non-negative
/// numbers.
/// - uppercase: Pass `true` to use uppercase letters or `false` to use
/// lowercase letters. The default is `false`.
@_semantics("constant_evaluable")
@inlinable
@_optimize(none)
public static func hex(
explicitPositiveSign: Bool = false,
uppercase: Bool = false
) -> OSLogFloatFormatting {
return OSLogFloatFormatting(
explicitPositiveSign: explicitPositiveSign,
uppercase: uppercase,
precision: nil,
notation: .hex
)
}
/// Displays an interpolated floating-point value in fprintf's `%e` format.
///
/// Prints the number in the form [-]d.ddd...dde±dd.
@_semantics("constant_evaluable")
@inlinable
@_optimize(none)
public static var exponential: OSLogFloatFormatting { .exponential() }
/// Displays an interpolated floating-point value in fprintf's `%e` format with
/// specified precision, and optional sign and case.
///
/// Prints the number in the form [-]d.ddd...dde±dd, with `precision` significant
/// digits following the radix point. Systems may impose an upper bound on the number
/// of digits that are supported.
///
/// All parameters except `precision` must be boolean literals.
///
/// - Parameters:
/// - precision: Number of digits to display after the radix point.
/// - explicitPositiveSign: Pass `true` to add a + sign to non-negative
/// numbers.
/// - uppercase: Pass `true` to use uppercase letters or `false` to use
/// lowercase letters. The default is `false`.
@_semantics("constant_evaluable")
@inlinable
@_optimize(none)
public static func exponential(
precision: @escaping @autoclosure () -> Int,
explicitPositiveSign: Bool = false,
uppercase: Bool = false
) -> OSLogFloatFormatting {
return OSLogFloatFormatting(
explicitPositiveSign: explicitPositiveSign,
uppercase: uppercase,
precision: precision,
notation: .exponential
)
}
/// Displays an interpolated floating-point value in fprintf's `%e` format with
/// an optional sign and case.
///
/// Prints the number in the form [-]d.ddd...dde±dd.
///
/// All parameters to this function must be boolean literals.
///
/// - Parameters:
/// - explicitPositiveSign: Pass `true` to add a + sign to non-negative
/// numbers.
/// - uppercase: Pass `true` to use uppercase letters or `false` to use
/// lowercase letters. The default is `false`.
@_semantics("constant_evaluable")
@inlinable
@_optimize(none)
public static func exponential(
explicitPositiveSign: Bool = false,
uppercase: Bool = false
) -> OSLogFloatFormatting {
return OSLogFloatFormatting(
explicitPositiveSign: explicitPositiveSign,
uppercase: uppercase,
precision: nil,
notation: .exponential
)
}
/// Displays an interpolated floating-point value in fprintf's `%g` format.
///
/// Behaves like `.fixed` when the number is scaled close to 1.0, and like
/// `.exponential` if it has a very large or small exponent.
@_semantics("constant_evaluable")
@inlinable
@_optimize(none)
public static var hybrid: OSLogFloatFormatting { .hybrid() }
/// Displays an interpolated floating-point value in fprintf's `%g` format with the
/// specified precision, and optional sign and case.
///
/// Behaves like `.fixed` when the number is scaled close to 1.0, and like
/// `.exponential` if it has a very large or small exponent.
///
/// All parameters except `precision` must be boolean literals.
///
/// - Parameters:
/// - precision: Number of digits to display after the radix point.
/// - explicitPositiveSign: Pass `true` to add a + sign to non-negative
/// numbers.
/// - uppercase: Pass `true` to use uppercase letters or `false` to use
/// lowercase letters. The default is `false`.
@_semantics("constant_evaluable")
@inlinable
@_optimize(none)
public static func hybrid(
precision: @escaping @autoclosure () -> Int,
explicitPositiveSign: Bool = false,
uppercase: Bool = false
) -> OSLogFloatFormatting {
return OSLogFloatFormatting(
explicitPositiveSign: explicitPositiveSign,
uppercase: uppercase,
precision: precision,
notation: .hybrid
)
}
/// Displays an interpolated floating-point value in fprintf's `%g` format with
/// optional sign and case.
///
/// Behaves like `.fixed` when the number is scaled close to 1.0, and like
/// `.exponential` if it has a very large or small exponent.
///
/// All parameters to this function must be boolean literals.
///
/// - Parameters:
/// - explicitPositiveSign: Pass `true` to add a + sign to non-negative
/// numbers.
/// - uppercase: Pass `true` to use uppercase letters or `false` to use
/// lowercase letters. The default is `false`.
@_semantics("constant_evaluable")
@inlinable
@_optimize(none)
public static func hybrid(
explicitPositiveSign: Bool = false,
uppercase: Bool = false
) -> OSLogFloatFormatting {
return OSLogFloatFormatting(
explicitPositiveSign: explicitPositiveSign,
uppercase: uppercase,
precision: nil,
notation: .hybrid
)
}
}
extension OSLogFloatFormatting {
/// Returns a fprintf-compatible length modifier for a given argument type
@_semantics("constant_evaluable")
@inlinable
@_optimize(none)
internal static func _formatStringLengthModifier<I: FloatingPoint>(
_ type: I.Type
) -> String? {
switch type {
// fprintf formatters promote Float to Double
case is Float.Type: return ""
case is Double.Type: return ""
#if !os(Windows) && (arch(i386) || arch(x86_64))
// fprintf formatters use L for Float80
case is Float80.Type: return "L"
#endif
default: return nil
}
}
/// Constructs an os_log format specifier for the given type `type`
/// using the specified alignment `align` and privacy qualifier `privacy`.
@_semantics("constant_evaluable")
@inlinable
@_optimize(none)
internal func formatSpecifier<I: FloatingPoint>(
for type: I.Type,
align: OSLogStringAlignment,
privacy: OSLogPrivacy
) -> String {
var specification = "%"
// Add privacy qualifier after % sign within curly braces. This is an
// os log specific flag.
if let privacySpecifier = privacy.privacySpecifier {
specification += "{"
specification += privacySpecifier
specification += "}"
}
// 1. Flags
// IEEE: `+` The result of a signed conversion shall always begin with a sign
// ( '+' or '-' )
if explicitPositiveSign {
specification += "+"
}
// IEEE: `-` The result of the conversion shall be left-justified within the field.
// The conversion is right-justified if this flag is not specified.
if case .start = align.anchor {
specification += "-"
}
if let _ = align.minimumColumnWidth {
// The alignment could be a dynamic value. Therefore, use a star here and pass it
// as an additional argument.
specification += "*"
}
if let _ = precision {
specification += ".*"
}
guard let lengthModifier =
OSLogFloatFormatting._formatStringLengthModifier(type) else {
fatalError("Float type has unknown length")
}
specification += lengthModifier
// 3. Precision and conversion specifier.
switch notation {
case .fixed:
specification += (uppercase ? "F" : "f")
case .exponential:
specification += (uppercase ? "E" : "e")
case .hybrid:
specification += (uppercase ? "G" : "g")
case .hex:
//guard type.radix == 2 else { return nil }
specification += (uppercase ? "A" : "a")
default:
fatalError("Unknown float notation")
}
return specification
}
}
|