From 03d7c43c22760aea7df7e96f18ee31ffd778a638 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Fri, 10 Mar 2023 19:26:43 -0800 Subject: [PATCH] _BString: Add index rounding methods to all views --- .../_BString+BidirectionalCollection.swift | 11 +++++++++++ .../BString/Views/_BString+UTF16View.swift | 11 +++++++++++ .../BString/Views/_BString+UTF8View.swift | 11 +++++++++++ .../BString/Views/_BString+UnicodeScalarView.swift | 11 +++++++++++ .../BString/Views/_BSubstring+UTF16View.swift | 13 +++++++++++++ .../BString/Views/_BSubstring+UTF8View.swift | 13 +++++++++++++ .../Views/_BSubstring+UnicodeScalarView.swift | 13 +++++++++++++ Sources/RopeModule/BString/Views/_BSubstring.swift | 13 +++++++++++++ 8 files changed, 96 insertions(+) diff --git a/Sources/RopeModule/BString/Conformances/_BString+BidirectionalCollection.swift b/Sources/RopeModule/BString/Conformances/_BString+BidirectionalCollection.swift index 77871d55..6e0c4840 100644 --- a/Sources/RopeModule/BString/Conformances/_BString+BidirectionalCollection.swift +++ b/Sources/RopeModule/BString/Conformances/_BString+BidirectionalCollection.swift @@ -61,4 +61,15 @@ extension _BString: BidirectionalCollection { } } +@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) +extension _BString { + internal func index(roundingDown i: Index) -> Index { + characterIndex(roundingDown: i) + } + + internal func index(roundingUp i: Index) -> Index { + characterIndex(roundingUp: i) + } +} + #endif diff --git a/Sources/RopeModule/BString/Views/_BString+UTF16View.swift b/Sources/RopeModule/BString/Views/_BString+UTF16View.swift index dcc05ff5..e57dee59 100644 --- a/Sources/RopeModule/BString/Views/_BString+UTF16View.swift +++ b/Sources/RopeModule/BString/Views/_BString+UTF16View.swift @@ -97,4 +97,15 @@ extension _BString.UTF16View: BidirectionalCollection { } } +@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) +extension _BString.UTF16View { + internal func index(roundingDown i: Index) -> Index { + _base.utf16Index(roundingDown: i) + } + + internal func index(roundingUp i: Index) -> Index { + _base.utf16Index(roundingUp: i) + } +} + #endif diff --git a/Sources/RopeModule/BString/Views/_BString+UTF8View.swift b/Sources/RopeModule/BString/Views/_BString+UTF8View.swift index ada4bd0f..57eadd6b 100644 --- a/Sources/RopeModule/BString/Views/_BString+UTF8View.swift +++ b/Sources/RopeModule/BString/Views/_BString+UTF8View.swift @@ -97,4 +97,15 @@ extension _BString.UTF8View: BidirectionalCollection { } } +@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) +extension _BString.UTF8View { + internal func index(roundingDown i: Index) -> Index { + _base.utf8Index(roundingDown: i) + } + + internal func index(roundingUp i: Index) -> Index { + _base.utf8Index(roundingUp: i) + } +} + #endif diff --git a/Sources/RopeModule/BString/Views/_BString+UnicodeScalarView.swift b/Sources/RopeModule/BString/Views/_BString+UnicodeScalarView.swift index addadd13..1ecd7b26 100644 --- a/Sources/RopeModule/BString/Views/_BString+UnicodeScalarView.swift +++ b/Sources/RopeModule/BString/Views/_BString+UnicodeScalarView.swift @@ -101,6 +101,17 @@ extension _BString.UnicodeScalarView: BidirectionalCollection { } } +@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) +extension _BString.UnicodeScalarView { + internal func index(roundingDown i: Index) -> Index { + _base.unicodeScalarIndex(roundingDown: i) + } + + internal func index(roundingUp i: Index) -> Index { + _base.unicodeScalarIndex(roundingUp: i) + } +} + @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) extension _BString.UnicodeScalarView: RangeReplaceableCollection { internal init() { diff --git a/Sources/RopeModule/BString/Views/_BSubstring+UTF16View.swift b/Sources/RopeModule/BString/Views/_BSubstring+UTF16View.swift index 2e1908f9..28876789 100644 --- a/Sources/RopeModule/BString/Views/_BSubstring+UTF16View.swift +++ b/Sources/RopeModule/BString/Views/_BSubstring+UTF16View.swift @@ -169,4 +169,17 @@ extension _BSubstring.UTF16View: BidirectionalCollection { } } +@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) +extension _BSubstring.UTF16View { + internal func index(roundingDown i: Index) -> Index { + precondition(i >= startIndex && i <= endIndex, "Index out of bounds") + return _base.utf16Index(roundingDown: i) + } + + internal func index(roundingUp i: Index) -> Index { + precondition(i >= startIndex && i <= endIndex, "Index out of bounds") + return _base.utf16Index(roundingUp: i) + } +} + #endif diff --git a/Sources/RopeModule/BString/Views/_BSubstring+UTF8View.swift b/Sources/RopeModule/BString/Views/_BSubstring+UTF8View.swift index 9de02d48..3a195739 100644 --- a/Sources/RopeModule/BString/Views/_BSubstring+UTF8View.swift +++ b/Sources/RopeModule/BString/Views/_BSubstring+UTF8View.swift @@ -143,4 +143,17 @@ extension _BSubstring.UTF8View: BidirectionalCollection { } } +@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) +extension _BSubstring.UTF8View { + internal func index(roundingDown i: Index) -> Index { + precondition(i >= startIndex && i <= endIndex, "Index out of bounds") + return _base.utf8Index(roundingDown: i) + } + + internal func index(roundingUp i: Index) -> Index { + precondition(i >= startIndex && i <= endIndex, "Index out of bounds") + return _base.utf8Index(roundingUp: i) + } +} + #endif diff --git a/Sources/RopeModule/BString/Views/_BSubstring+UnicodeScalarView.swift b/Sources/RopeModule/BString/Views/_BSubstring+UnicodeScalarView.swift index 581f5484..3e7a4271 100644 --- a/Sources/RopeModule/BString/Views/_BSubstring+UnicodeScalarView.swift +++ b/Sources/RopeModule/BString/Views/_BSubstring+UnicodeScalarView.swift @@ -150,6 +150,19 @@ extension _BSubstring.UnicodeScalarView: BidirectionalCollection { } } +@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) +extension _BSubstring.UnicodeScalarView { + internal func index(roundingDown i: Index) -> Index { + precondition(i >= startIndex && i <= endIndex, "Index out of bounds") + return _base.unicodeScalarIndex(roundingDown: i) + } + + internal func index(roundingUp i: Index) -> Index { + precondition(i >= startIndex && i <= endIndex, "Index out of bounds") + return _base.unicodeScalarIndex(roundingUp: i) + } +} + @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) extension _BSubstring.UnicodeScalarView { /// Run the closure `body` to mutate the contents of this view within `range`, then update diff --git a/Sources/RopeModule/BString/Views/_BSubstring.swift b/Sources/RopeModule/BString/Views/_BSubstring.swift index b4b46602..a975678f 100644 --- a/Sources/RopeModule/BString/Views/_BSubstring.swift +++ b/Sources/RopeModule/BString/Views/_BSubstring.swift @@ -213,6 +213,19 @@ extension _BSubstring: BidirectionalCollection { } } +@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) +extension _BSubstring { + internal func index(roundingDown i: Index) -> Index { + precondition(i >= startIndex && i <= endIndex, "Index out of bounds") + return _base.characterIndex(roundingDown: i) + } + + internal func index(roundingUp i: Index) -> Index { + precondition(i >= startIndex && i <= endIndex, "Index out of bounds") + return _base.characterIndex(roundingUp: i) + } +} + @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) extension _BSubstring { /// Run the closure `body` to mutate the contents of this view within `range`, then update