High Performance UTF8 for Swift

Related tags

Utility Hitch
Overview

High Performance UTF8 for Swift

Consider Hitch as an alternative to String.

+-------------------------------+--------------------------+
|HitchPerformanceTests.swift    |    Faster than String    |
+-------------------------------+--------------------------+
|string iterator                |         5718.35x         |
|utf8 iterator                  |          94.80x          |
|last index of                  |          40.68x          |
|first index of                 |          18.72x          |
|contains                       |          14.38x          |
|uppercase/lowercase            |          14.25x          |
|replace occurrences of         |          10.64x          |
|append (dynamic capacity)      |          5.64x           |
|append (static capacity)       |          1.14x           |
+-------------------------------+--------------------------+

Usage

The guiding vision for Hitch is to help ensure you are always on the fastest path for UTF8 string processing. It aims to be blatantly transparent about hidden copy operations that can slow you down unintentionally.

A Hitch is a reference type which holds UTF8 buffer data; think of it as a hybrid of NSString and String. It is a class, it can point to shared mutable or immutable data, and it has copyOnWrite disabled by default.

A HalfHitch is a value type which points to existing, immuable UTF8 data. If possible, it will retain the original source it is pointing to.

Note: HalfHitch.unescape() is the one exception to this, which will allow you to unescape unicode in place if the HalfHitch is pointing to a section of mutable data. It is coded to fatal error if you attempt to call it on immutable data.

The Hitchable protocol provides all of the immutable funcationality shared between Hitches and HalfHitches. You could conform to it for your own structures to provide said functionality to any arbitrary UTF8 data.

As an example of this, prior to v0.4.0 Hitch's implementation ExpressibleByStringLiteral would result in n opaque copy of the underlying data. Now ExpressibleByStringLiteral is only allowed for StaticString (ie a string literal), where the resulting Hitchable can point to the raw, unmutable strind data.

By default, Hitchables which are generated off of StaticStrings have "copyOnWrite" disabled. This means that attempting call a mutating method on a Hitch created from the StaticString will result in a fatal error. You can enabled copyOnWrite, in which case calling a mutating method on the Hitch will result in a new, mutable data store to be created prior to the mutation taking place.

Format Strings

Hitch has its own high performance formatted strings. It works like this:

Example:

<<< ["hello", "world"] XCTAssertEqual(halfHitch, "hello world") XCTAssertEqual(hitch, "hello world")">
let halfHitch = "{0} {1}" << ["hello", "world"]
let hitch = "{0} {1}" <<< ["hello", "world"]
    
XCTAssertEqual(halfHitch, "hello world")
XCTAssertEqual(hitch, "hello world")

Example:

let value = Hitch("""
    {0}
    +----------+----------+----------+
    |{-??     }|{~?      }|{?       }|
    |{-?      }|{~?      }|{+?      }|
    |{-?.2    }|{~8.3    }|{+?.1    }|
    |{-1      }|{~2      }|{1       }|
    +----------+----------+----------+
    {{we no longer escape braces}}
    {These don't need to be escaped because they contain invalid characters}
    """, "This is an unbounded field", "Hello", "World", 27, 1, 2, 3, 1.0/3.0, 543.0/23.0, 99999.99999)
print(value)

Output:

This is an unbounded field
+----------+----------+----------+
|Hello     |  World   |        27|
|1         |    2     |         3|
|0.33      |  23.608  |      23.6|
|Hello     |  World   |     Hello|
+----------+----------+----------+
{{we no longer escape braces}}
{These don't need to be escaped because they contain invalid characters}

An unbounded field
is defined by {0} with no spaces or other formatting.

Field width
is defined by the amount of space between the opening { and the closing }. As you can see in the example above, this makes everything visually line up in the format string.

Left justification
is defined by a - sign, such as {-0 }. The sign can appear anywhere in the field, such as { 0 - }

Center justification
is defined by a ~ sign, such as {~0 } The sign can appear anywhere in the field, such as {~ 0 }

Right justification
is defined by a + sign, such as {+0 } The sign can appear anywhere in the field, such as { 0 +}

Unnamed value indexes
are allowed by using a ?. This value starts at 0, and is incremented after every ? encountered inside of a {}. So "{??? }" is valid, you will get the 2 indexed value in this field.

Braces can still be used normally;
if illegal characters exist inside of a brace, or the braces fail to contain the required information for formatting, then they are interpretted as is.

Floating point precision
is defined by placing . after the value index, followed by the number of precision digits. For example, {-?.4 } means "left aligned using the next value index with four points of decimal precision with a field width of 8"

Hitch License

Hitch is free software distributed under the terms of the MIT license, reproduced below. Hitch may be used for any purpose, including commercial purposes, at absolutely no cost. No paperwork, no royalties, no GNU-like "copyleft" restrictions. Just download and enjoy.

Copyright (c) 2020 Chimera Software, LLC

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

You might also like...
Swift-compute-runtime - Swift runtime for Fastly Compute@Edge

swift-compute-runtime Swift runtime for Fastly Compute@Edge Getting Started Crea

Swift-HorizontalPickerView - Customizable horizontal picker view component written in Swift for UIKit/iOS

Horizontal Picker View Customizable horizontal picker view component written in

swift-highlight a pure-Swift data structure library designed for server applications that need to store a lot of styled text

swift-highlight is a pure-Swift data structure library designed for server applications that need to store a lot of styled text. The Highlight module is memory-efficient and uses slab allocations and small-string optimizations to pack large amounts of styled text into a small amount of memory, while still supporting efficient traversal through the Sequence protocol.

Sovran-Swift: Small, efficient, easy. State Management for Swift

Sovran-Swift: Small, efficient, easy. State Management for Swift

Approximate is a Swift package that provides implementations of floating point comparisons for the Swift ecosystem

Approximate Approximate floating point equality comparisons for the Swift Programming Language. Introduction Approximate is a Swift package that provi

A Swift app, named 'iPose', for iPhone's pose measurement based on Swift.

iPhone's pose measurement based on Swift. This is a Swift app, named 'iPose', for iPhone's pose measurement based on Swift. This is a side project to

Swift Package Manager plugin which runs ActionBuilder to create a Github Actions workflow for a swift package.

ActionBuilderPlugin A Swift Package Manager command which builds a Github Actions workflow for the current package. By default the workflow file will

Swift Server Implementation - RESTful APIs, AWS Lambda Serverless For Swift Runtime amazonlinux: AWS Lambda + API Gateway
Swift Server Implementation - RESTful APIs, AWS Lambda Serverless For Swift Runtime amazonlinux: AWS Lambda + API Gateway

Swift Server Implementation - RESTful APIs, AWS Lambda Serverless For Swift Runtime amazonlinux: AWS Lambda + API Gateway deployed on Graviton arm64 build swift:5.6.2-amazonlinux2-docker image

A simple swift package that provides a Swift Concurrency equivalent to `@Published`.

AsyncValue This is a simple package that provides a convenience property wrapper around AsyncStream that behaves almost identically to @Published. Ins

Owner
Rocco Bowling
Rocco Bowling
Lock a terminal command to the efficiency or performance cores on a big.LITTLE ARM processor

CPU-Lock Lock a terminal command to the efficiency or performance cores on a big.LITTLE ARM processor (Designed for Apple Silicon). Usage Download the

BitesPotatoBacks 0 Aug 11, 2022
BCSwiftTor - Opinionated pure Swift controller for Tor, including full support for Swift 5.5 and Swift Concurrency

BCSwiftTor Opinionated pure Swift controller for Tor, including full support for

Blockchain Commons, LLC — A “not-for-profit” benefit corporation 4 Oct 6, 2022
Swift Markdown is a Swift package for parsing, building, editing, and analyzing Markdown documents.

Swift Markdown is a Swift package for parsing, building, editing, and analyzing Markdown documents.

Apple 2k Dec 28, 2022
Swift-DocC is a documentation compiler for Swift frameworks and packages aimed at making it easy to write and publish great developer documentation.

Swift-DocC is a documentation compiler for Swift frameworks and packages aimed at making it easy to write and publish great developer docum

Apple 833 Jan 3, 2023
Cross-Platform, Protocol-Oriented Programming base library to complement the Swift Standard Library. (Pure Swift, Supports Linux)

SwiftFoundation Cross-Platform, Protocol-Oriented Programming base library to complement the Swift Standard Library. Goals Provide a cross-platform in

null 620 Oct 11, 2022
Swift - ✏️Swift 공부 저장소✏️

Swift 스위프트의 기초 1. Swift의 기본 2. 변수와 상수 [3. 데이터 타입 기본] [4. 데이터 타입 고급] 5. 연산자 6. 흐름 제어 7. 함수 8. 옵셔널 객체지향 프로그래밍과 스위프트 9. 구조체와 클래스 10. 프로퍼티와 메서드 11. 인스턴스 생

Jiwon 0 Mar 9, 2022
Swift-ndi - Swift wrapper around NewTek's NDI SDK

swift-ndi Swift wrapper around NewTek's NDI SDK. Make sure you extracted latest

Alessio Nossa 12 Dec 29, 2022
__.swift is a port of Underscore.js to Swift.

__.swift Now, __.swift is version 0.2.0! With the chain of methods, __.swift became more flexible and extensible. Documentation: http://lotz84.github.

Tatsuya Hirose 86 Jun 29, 2022
SNTabBarDemo-Swift - Cool TabBar With Swift

SNTabBarDemo-Swift Cool TabBar How To Use // MARK: - setup private func setu

iAnchor 3 Sep 29, 2022
Swift-when - Expression switch support in Swift

Swift When - supporting switch expressions in Swift! What is it? Basically, it a

Gordan Glavaš 7 Nov 24, 2022