Skip to content

Instantly share code, notes, and snippets.

extension String {
public func _compareStringLinux(_ rhs: String) -> Int {
// Fast lexigraphic comparison of (composed) pre-normalized prefixes
var lhsCore = self._core
var rhsCore = rhs._core
let minCount = Swift.min(lhsCore.count, rhsCore.count)
for idx in 0..<minCount {
// Detect when pre-normalization ends: Quick check if next code unit might be combining
if idx+1 < minCount && (lhsCore[idx+1] >= 0x300 || rhsCore[idx+1] >= 0x300) {
@milseman
milseman / Swift5StateOfString.md
Created January 10, 2018 19:49
State of String: ABI, Performance, Ergonomics, and You!

State of String: ABI, Performance, Ergonomics, and You!

Hello, I’ve been working on implementing, optimizing, and improving String in preparation for ABI stability, and I thought I’d share the current status of String in Swift 5 and some potential directions to go. This is the product of conversations with open source contributors and my colleagues on the Swift standard library team at Apple.

The primary area of focus is stabilizing String’s ABI, but we’d be remiss if we couldn’t also fit in performance and ergonomic improvements. String’s ergonomics in particular is one area where we think the status quo is woefully inadequate, and over half of this email is devoted to that topic. At the end, there’s a section about a community initiative that we hope can help users of String as well as guide future development.

(Note: I’m sending this to swift-dev because much of the contents revolve around implementation concerns. I’ll also cross-reference on swift-evolution and swift-users. See also the [StringManife

struct S1 {
var stored: UInt?
init(_ x: UInt) {
if x > 42 {
self.init(_big: x)
return
}
self.stored = x // <--- error: 'self' used before 'self.init' call or assignment to 'self'
}
@milseman
milseman / eat_seek_peek.swift
Created February 28, 2018 20:26
Playground for Self-sliced Collections: eat/seek/peek
// Eat/seek/peek
extension Collection where SubSequence == Self, Element: Equatable {
mutating func eat() -> Element {
defer { self = self.dropFirst() }
return peek()
}
mutating func eat(_ n: Int) -> SubSequence {
let (pre, rest) = self.seek(n)
defer { self = rest }
@milseman
milseman / char_props.md
Last active May 4, 2018 20:53
Pitch: Character and String properties
@milseman
milseman / ExpressibleByStringInterpolation.swift
Last active April 12, 2018 01:11
ExpressiblyByStringInterpolation Alternative Formulation
protocol ESI {
// Interpolate a segment
mutating func addSegment<T>(_ t: T)
// Interpolate a literal
mutating func addLiteral(_ s: String)
// Compiler will produce closure to pass to this init. Closure will consist
// of calls to writeSegment and writeLiteral
init(performingInterpolation: (inout Self) -> ())
@milseman
milseman / Whitespace_visibility.swift
Last active May 23, 2018 17:53
Whitespace visibility example
let str1 = "a\u{1680}b"
let str2 = "a\u{0020}b"
print(str1) // "a b"
print(str2) // "a b"
@milseman
milseman / substring_benchmark.swift
Last active October 15, 2018 00:19
Extracted from Substring.swift benchmark
print("start")
public var i = 0
// @inline(never)
// public func blackHole<T>(_ x: T) {
// i += 1
// }
@inline(never)
@milseman
milseman / malloc_size.md
Created October 23, 2018 14:44
Malloc size experiment
var i = 0
while i <= 0x4_0000 { 
  let size = malloc_size(malloc(i+1)) 
  print(" \(i+1) => \(size) (\(size - i))") 
  i = size 
}

64-bit MacOS:

@milseman
milseman / string_index_explicit_offsets.swift
Last active January 29, 2019 21:39
String Index Explicit Offsets
extension String.Index {
/// Creates a new index at the specified UTF-16 code unit offset
///
/// - Parameter offset: An offset in UTF-16 code units.
public init(offset: Int, within utf16: String.UTF16View) {
let (start, end) = (utf16.startIndex, utf16.endIndex)
guard offset >= 0,
let idx = utf16.index(start, offsetBy: offset, limitedBy: end)
else {
self = end