Files
swift-mirror/utils/gen-unicode-data/Sources/GenUtils/Formatting.swift
Alejandro Alonso 3b402f0179 [stdlib] Implement native Unicode.Scalar binary properties (#39597)
* Generate Unicode data for Scalar Binary Properties

* Use native scalar binary property lookup

* Add _BinaryProperties to Scalar Properties

narrow access control

* Upgrade the notice to a warning in UnicodeScalarProperties
2021-11-15 15:20:46 -08:00

44 lines
1.2 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021 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
//
//===----------------------------------------------------------------------===//
// Given a collection, format it into a string within 80 columns and fitting as
// many elements in a row as possible.
public func formatCollection<C: Collection>(
_ c: C,
into result: inout String,
using handler: (C.Element) -> String
) {
// Our row length always starts at 2 for the initial indentation.
var rowLength = 2
for element in c {
let string = handler(element)
if rowLength == 2 {
result += " "
}
if rowLength + string.count + 1 > 100 {
result += "\n "
rowLength = 2
} else {
result += rowLength == 2 ? "" : " "
}
result += "\(string),"
// string.count + , + space
rowLength += string.count + 1 + 1
}
}