Last active
September 23, 2024 05:50
-
-
Save brettohland/f07fa1069e495d96dda098f13adaefae to your computer and use it in GitHub Desktop.
Revisions
-
brettohland revised this gist
Jul 31, 2022 . 1 changed file with 31 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,11 +4,36 @@ import Foundation if #available(iOS 16.0, *) { do { try URL.FormatStyle.Strategy(port: .defaultValue(80)).parse("http://www.apple.com") // http://www.apple.com:80 try URL.FormatStyle.Strategy(port: .optional).parse("http://www.apple.com") // http://www.apple.com try? URL.FormatStyle.Strategy(port: .required).parse("http://www.apple.com") // nil // This returns a valid URL try URL.FormatStyle.Strategy() .scheme(.required) .user(.required) .password(.required) .host(.required) .port(.required) .path(.required) .query(.required) .fragment(.required) .parse("https://jAppleseed:Test1234@apple.com:80/macbook-pro?get-free#someFragmentOfSomething") // This throws an error try URL.FormatStyle.Strategy() .scheme(.required) .user(.required) .password(.required) .host(.required) .port(.required) .path(.required) .query(.required) .fragment(.required) .parse("https://jAppleseed:Test1234@apple.com/macbook-pro?get-free#someFragmentOfSomething") } catch { print(error) } } -
brettohland created this gist
Jun 14, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ # ParseableFormatStyle & You [See the blog post](https://ampersandsoftworks.com/parsing-strings-with-parseableformatstyle) [Download the Xcode Playground](https://github.com/brettohland/ParseableFormatStyle-Examples) [](https://ko-fi.com/Q5Q6BLZHQ) --- Another questionable project by [Ampersand Softworks](https://ampersandsoftworks.com) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,124 @@ import UIKit // MARK: - Parsing Decimals try? Decimal.FormatStyle().notation(.scientific).parseStrategy.parse("1E5") // 100000 try? Decimal.FormatStyle().scale(5).notation(.scientific).parseStrategy.parse("1E5") // 20000 try? Decimal.FormatStyle().scale(-5).notation(.scientific).parseStrategy.parse("1E5") // -20000 try? Decimal.FormatStyle.Percent(locale: Locale(identifier: "fr_FR")).parseStrategy.parse("1E5") // 100000 try? Decimal.FormatStyle.Percent(locale: Locale(identifier: "en_CA")).parseStrategy.parse("1E5") // 100000 try? Decimal.FormatStyle.Percent().parseStrategy.parse("15%") // 0.15 try? Decimal.FormatStyle.Percent().scale(2).parseStrategy.parse("100%") // 50 try? Decimal.FormatStyle.Percent(locale: Locale(identifier: "fr_FR")).parseStrategy.parse("15 %") // 0.15 try? Decimal.FormatStyle.Percent(locale: Locale(identifier: "en_CA")).parseStrategy.parse("15 %") // 0.15 try? Decimal.FormatStyle.Currency(code: "GBP") .presentation(.fullName) .parseStrategy.parse("10.00 British pounds") // 10 try? Decimal.FormatStyle.Currency(code: "GBP", locale: Locale(identifier: "fr_FR")) .presentation(.fullName) .parseStrategy.parse("10,00 livres sterling") // 10 try? Decimal.FormatStyle.Currency(code: "GBP") .presentation(.fullName) .locale(Locale(identifier: "fr_FR")) .parseStrategy.parse("10,00 livres sterling") // 10 try? Decimal("1E5", strategy: Decimal.FormatStyle().notation(.scientific).parseStrategy) // 100000 try? Decimal("1E5", format: Decimal.FormatStyle().notation(.scientific)) // 100000 try? Decimal("15%", strategy: Decimal.FormatStyle.Percent().parseStrategy) try? Decimal("15%", format: Decimal.FormatStyle.Percent()) try? Decimal("10.00 British pounds", strategy: Decimal.FormatStyle.Currency(code: "GBP").parseStrategy) // 10 try? Decimal("10.00 British pounds", format: Decimal.FormatStyle.Currency(code: "GBP")) // 10 // MARK: - Parsing Dates try? Date.FormatStyle() .day() .month() .year() .hour() .minute() .second() .parse("Feb 22, 2022, 2:22:22 AM") // Feb 22, 2022, 2:22:22 AM try? Date.FormatStyle() .day() .month() .year() .hour() .minute() .second() .parseStrategy.parse("Feb 22, 2022, 2:22:22 AM") // Feb 22, 2022, 2:22:22 AM try? Date.ISO8601FormatStyle(timeZone: TimeZone(secondsFromGMT: 0)!) .year() .day() .month() .dateSeparator(.dash) .dateTimeSeparator(.standard) .timeSeparator(.colon) .timeZoneSeparator(.colon) .time(includingFractionalSeconds: true) .parse("2022-02-22T09:22:22.000") // Feb 22, 2022, 2:22:22 AM try? Date.ISO8601FormatStyle(timeZone: TimeZone(secondsFromGMT: 0)!) .year() .day() .month() .dateSeparator(.dash) .dateTimeSeparator(.standard) .timeSeparator(.colon) .timeZoneSeparator(.colon) .time(includingFractionalSeconds: true) .parseStrategy.parse("2022-02-22T09:22:22.000") // Feb 22, 2022, 2:22:22 AM try? Date( "Feb 22, 2022, 2:22:22 AM", strategy: Date.FormatStyle().day().month().year().hour().minute().second().parseStrategy ) // Feb 22, 2022 at 2:22 AM try? Date( "2022-02-22T09:22:22.000", strategy: Date.ISO8601FormatStyle(timeZone: TimeZone(secondsFromGMT: 0)!) .year() .day() .month() .dateSeparator(.dash) .dateTimeSeparator(.standard) .timeSeparator(.colon) .timeZoneSeparator(.colon) .time(includingFractionalSeconds: true) .parseStrategy ) // Feb 22, 2022 at 2:22 AM // MARK: - Parsing Person Names // namePrefix: Dr givenName: Elizabeth middleName: Jillian familyName: Smith nameSuffix: Esq. try? PersonNameComponents.FormatStyle() .parseStrategy.parse("Dr Elizabeth Jillian Smith Esq.") // namePrefix: Dr givenName: Elizabeth middleName: Jillian familyName: Smith nameSuffix: Esq. try? PersonNameComponents.FormatStyle(style: .long) .parseStrategy.parse("Dr Elizabeth Jillian Smith Esq.") // namePrefix: Dr givenName: Elizabeth middleName: Jillian familyName: Smith nameSuffix: Esq. try? PersonNameComponents.FormatStyle(style: .long, locale: Locale(identifier: "zh_CN")) .parseStrategy.parse("Dr Smith Elizabeth Jillian Esq.") // namePrefix: Dr givenName: Elizabeth middleName: Jillian familyName: Smith nameSuffix: Esq. try? PersonNameComponents.FormatStyle(style: .long) .locale(Locale(identifier: "zh_CN")) .parseStrategy.parse("Dr Smith Elizabeth Jillian Esq.") // namePrefix: Dr givenName: Elizabeth middleName: Jillian familyName: Smith nameSuffix: Esq. try? PersonNameComponents( "Dr Elizabeth Jillian Smith Esq.", strategy: PersonNameComponents.FormatStyle(style: .long).parseStrategy ) rse This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ import Foundation // MARK: - URLs (iOS 16/Xcode 14 only) if #available(iOS 16.0, *) { try? URL.FormatStyle() .parseStrategy.parse("https://jAppleseed:Test1234@apple.com:80/macbook-pro?get-free#someFragmentOfSomething") try? URL( "https://jAppleseed:Test1234@apple.com:80/macbook-pro?get-free#someFragmentOfSomething", strategy: URL.FormatStyle().parseStrategy ) }