From 6a7f53c3da992cdb03097c0b7cc149fc4be4e123 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Fri, 10 Mar 2023 20:17:00 -0800 Subject: [PATCH] _BString: Add conversion initializers from all views --- .../BString/Views/_BString+UTF16View.swift | 4 ++++ .../BString/Views/_BString+UTF8View.swift | 4 ++++ .../BString/Views/_BSubstring+UTF16View.swift | 13 +++++++++++++ .../BString/Views/_BSubstring+UTF8View.swift | 13 +++++++++++++ .../Views/_BSubstring+UnicodeScalarView.swift | 4 ++-- 5 files changed, 36 insertions(+), 2 deletions(-) diff --git a/Sources/RopeModule/BString/Views/_BString+UTF16View.swift b/Sources/RopeModule/BString/Views/_BString+UTF16View.swift index e57dee59..faf34b2d 100644 --- a/Sources/RopeModule/BString/Views/_BString+UTF16View.swift +++ b/Sources/RopeModule/BString/Views/_BString+UTF16View.swift @@ -26,6 +26,10 @@ extension _BString { internal var utf16: UTF16View { UTF16View(_base: self) } + + internal init(_ utf16: UTF16View) { + self = utf16._base + } } @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) diff --git a/Sources/RopeModule/BString/Views/_BString+UTF8View.swift b/Sources/RopeModule/BString/Views/_BString+UTF8View.swift index 57eadd6b..ccdc4646 100644 --- a/Sources/RopeModule/BString/Views/_BString+UTF8View.swift +++ b/Sources/RopeModule/BString/Views/_BString+UTF8View.swift @@ -26,6 +26,10 @@ extension _BString { internal var utf8: UTF8View { UTF8View(_base: self) } + + internal init(_ utf8: UTF8View) { + self = utf8._base + } } @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) diff --git a/Sources/RopeModule/BString/Views/_BSubstring+UTF16View.swift b/Sources/RopeModule/BString/Views/_BSubstring+UTF16View.swift index 28876789..e16b68e3 100644 --- a/Sources/RopeModule/BString/Views/_BSubstring+UTF16View.swift +++ b/Sources/RopeModule/BString/Views/_BSubstring+UTF16View.swift @@ -39,6 +39,19 @@ extension _BSubstring { } } +@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) +extension _BString { + internal init?(_ utf16: _BSubstring.UTF16View) { + guard + !utf16.startIndex._isUTF16TrailingSurrogate, + !utf16.endIndex._isUTF16TrailingSurrogate + else { + return nil + } + self.init(_from: utf16._base, in: utf16._bounds) + } +} + @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) extension _BSubstring.UTF16View { internal var base: _BString.UTF16View { _base.utf16 } diff --git a/Sources/RopeModule/BString/Views/_BSubstring+UTF8View.swift b/Sources/RopeModule/BString/Views/_BSubstring+UTF8View.swift index 3a195739..f8c83458 100644 --- a/Sources/RopeModule/BString/Views/_BSubstring+UTF8View.swift +++ b/Sources/RopeModule/BString/Views/_BSubstring+UTF8View.swift @@ -39,6 +39,19 @@ extension _BSubstring { } } +@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) +extension _BString { + internal init?(_ utf8: _BSubstring.UTF8View) { + guard + utf8._base.unicodeScalars.index(roundingDown: utf8.startIndex) == utf8.startIndex, + utf8._base.unicodeScalars.index(roundingDown: utf8.endIndex) == utf8.endIndex + else { + return nil + } + self.init(_from: utf8._base, in: utf8._bounds) + } +} + @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) extension _BSubstring.UTF8View { internal var base: _BString.UTF8View { _base.utf8 } diff --git a/Sources/RopeModule/BString/Views/_BSubstring+UnicodeScalarView.swift b/Sources/RopeModule/BString/Views/_BSubstring+UnicodeScalarView.swift index 3e7a4271..212fefe5 100644 --- a/Sources/RopeModule/BString/Views/_BSubstring+UnicodeScalarView.swift +++ b/Sources/RopeModule/BString/Views/_BSubstring+UnicodeScalarView.swift @@ -41,8 +41,8 @@ extension _BSubstring { @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) extension _BString { - internal init(_ view: _BSubstring.UnicodeScalarView) { - self.init(_from: view._base, in: view._bounds) + internal init(_ unicodeScalars: _BSubstring.UnicodeScalarView) { + self.init(_from: unicodeScalars._base, in: unicodeScalars._bounds) } }