Commit Graph

377 Commits

Author SHA1 Message Date
Doug Gregor 453277eb74 Mark the various with* functions as @safe
Functions like withUnsafeBufferPointer are, by themselves, safe to
call. It's only the operations on the unsafe pointers passed into the
closure that are the safety issue.

This was the intent spelled out in SE-0458 but was not fully realized
in the library.

Fixes rdar://174519372.
2026-04-16 22:37:54 -07:00
Nate Cook 26a61e20e6 Fix grammatical error in String.swift documentation (#87934)
rdar://171958960
2026-03-19 16:12:23 -07:00
Clinton Nkwocha 2385488c84 Generalize String functions for typed throws (#87495)
Generalizes `String`:
- `init(unsafeUninitializedCapacity:initializingUTF8With:)`
- `withCString(_:)`
- `withCString(encodedAs:_:)`
- `withUTF8(_:)`
- `_withNFCCodeUnits(_:)`

and `Substring`:
  - `withCString(_:)`
  - `withCString(encodedAs:_:)`
  - `withUTF8(_:)`

for typed throws.
2026-03-10 11:27:06 +00:00
Doug Gregor 1e8317803e Make Bool's conformance to LosslessStringConvertible not use String.==
String.== performs normalization, which brings in the Unicode tables.
Don't use it for Bool's conformance to LosslessStringConvertible,
which doesn't need it. It's only the "true" and "false" strings that
matter.
2026-02-06 14:10:38 -08:00
Karoy Lorentey e5787c661d [stdlib] String, Substring: Use new _StringGuts helper 2025-12-08 17:32:20 -08:00
Karoy Lorentey c8202c6f9a Apply suggestions from code review 2025-12-08 17:32:19 -08:00
Karoy Lorentey b7e32db453 [stdlib] Update availability of isTriviallyIdentical methods 2025-12-08 17:32:19 -08:00
Rick van Voorden abae2f6547 [stdlib] string identical
Co-Authored-By: Karoy Lorentey <klorentey@apple.com>
2025-12-08 17:32:19 -08:00
Michael Ilseman e6e4bd6056 UTF8Span (#78531)
Add support for UTF8Span

Also, refactor validation and grapheme breaking
2025-04-11 16:11:11 -06:00
Doug Gregor 22eecacc35 Adopt unsafe annotations throughout the standard library 2025-02-26 14:28:01 -08:00
Guillaume Lessard d4ae6b0ce8 [gardening] move String.withCString to String.swift
- it has been at an unexpected location for unremembered reasons.
2025-01-09 22:16:58 -08:00
Guillaume Lessard 29a4b8abff Merge pull request #75622 from glessard/rdar132941798-aEIC-private-consternation 2024-08-06 20:31:04 -07:00
Guillaume Lessard dd854faad1 [stdlib] use temporary rebinding as appropriate 2024-08-01 16:26:34 -07:00
Guillaume Lessard a77b8cea70 [stdlib] alter inconsistent declarations
`private` declarations should never be allowed to carry the `@_alwaysEmitIntoClient` attribute. This has been allowed by mistake for a while, but newer compilers should make this an error.
2024-08-01 16:26:34 -07:00
Ben Rimmington 66e6c009eb [stdlib] Move unused functions to LegacyABI.swift (#75462) 2024-07-27 04:45:31 +01:00
Karl 7a57bd8ae4 [stdlib] Refactor Unicode normalization (#73590)
* [stdlib] Refactor Unicode normalization

* Tweak inlining
2024-05-31 08:22:37 -06:00
Kuba Mracek 7ae20b7039 [embedded] Port Swift.String to embedded Swift 2024-05-08 11:11:37 -07:00
Mishal Shah 96d61d3c5c Merge remote-tracking branch 'origin/main' into bump-swift-version-to-6 2024-02-20 14:31:55 -08:00
Mishal Shah af112c1591 Update the Swift version to 6.0 from 5.11 2024-02-19 17:47:16 -08:00
Alejandro Alonso aadf4a13a2 Do a fast NFC check over the slice instead of guts 2024-02-08 10:50:03 -08:00
Guillaume Lessard 8d0991f9d0 [se-0405] improve examples in documentation 2024-01-10 14:32:26 -08:00
Guillaume Lessard b869a3cabe [se-0405] improve readability of double-optional unwrapping 2023-12-21 10:44:52 -08:00
Guillaume Lessard 566fbf4fec [se-0405] update availability to a realistic release target 2023-12-21 10:44:52 -08:00
Guillaume Lessard 92df9b4bdb Apply suggestions from code review
Co-authored-by: Ben Rimmington <me@benrimmington.com>
2023-12-21 10:44:52 -08:00
Guillaume Lessard f7006880c7 [se-0405] adapt implementation from staging package 2023-12-21 10:44:52 -08:00
Ben Rimmington 98ada1b200 [stdlib] Fix buffer size of small-capacity strings (#67929) 2023-08-16 10:00:42 +01:00
Nate Chandler cda365ca8d [stdlib] Collection types are eagerMove.
Types that have "value semantics" should not have lexical lifetimes.
Value types are not expected to have custom deinits. Are not expected to
expose unsafe interior pointers. And cannot have weak references because
they are structs. Therefore, deinitialization barriers are irrelevant.

rdar://107076869
2023-03-30 11:04:47 -07:00
Karoy Lorentey 108bc0e7b2 [stdlib] Add String._isIdentical(to:)
rdar://104828814
2023-01-30 12:08:35 -08:00
Karoy Lorentey 1241df3fab [stdlib] String.debugDescription: Fix quoting behavior
`String.debugDescription` currently fails to protect the contents of
the string from combining with the opening or closing `”` characters
or one of the characters of a quoted scalar:

```swift
let s = “\u{301}A\n\u{302}B\u{70F}”
print(s.debugDescription)
// ⟹ “́A\n̂B܏”  (characters: “́, A, \, n̂, B, ܏”)
```

This can make debug output difficult to read, as string contents are
allowed to spread over and pollute neighboring meta-characters.

This change fixes this by force-quoting the problematic scalars in
these cases:

```swift
let s = “\u{301}A\n\u{302}B\u{70F}”
print(s.debugDescription)
// ⟹ “\u{301}A\n\u{302}B\u{70F}”
```

Of course, Unicode scalars that don’t engage in such behavior are
still allowed to pass through unchanged:

```swift
let s = “Cafe\u{301}”
print(s.debugDescription)
// ⟹ “Café”
```
2023-01-16 01:15:39 -08:00
David Smith 9dd8d3ad1a Actually slice things in _StringGutsSlice 2022-05-17 14:31:11 -07:00
Alejandro Alonso c9115e1cec Create wrapper types with availability NFD and NFC
Swap the bases to unicodeScalarView
2022-04-05 09:06:32 -07:00
Alejandro Alonso ac6c08f157 [stdlib] Make the rest of the scalar properties native (#40233)
* Factor out the scalar bit array index mechanism

* Implement native numeric scalar properties

* Implement native scalar name aliases

* Implement native scalar mappings

* Implement native scalar names

* Implement native scalar age

* Implement native scalar general category

* Address Michael's and others comments

fix special mappings

fix bug
2021-11-30 12:40:32 -08:00
Karoy Lorentey 6d255658ca Merge pull request #39994 from lorentey/adopt-availability-macros
[stdlib] Adopt availability macros
2021-11-01 14:45:33 -07:00
Karoy Lorentey 8ed81ae063 [stdlib] Adopt availability macros 2021-10-31 15:00:58 -07:00
Alex Martini 1186fc7f13 Remove "iff" from doc comments.
This abbreviation for "if and only if" is confusing to those not coming
from a background in formal mathematics, and is frequently reported as a
type by developers reading the documentation.

This commit also changes doc comments in internal and private members,
which don't become part of the public documentation, because omitting
"iff" everywhere makes it much easier to check for any later changes
that reintroduce it.
2021-10-29 10:16:27 -07:00
Alejandro Alonso 014e822cb2 Address Michael's comments
fix infinite recursion bug

NFC: Remove early ccc check

remember that false is turned on
2021-09-29 14:20:22 -07:00
Alejandro Alonso 98aaa157ec Implement native normalization for String
use >/< instead of !=

fix some bugs

fix
2021-09-29 14:20:21 -07:00
heoblitz 20ffc036ed Add whitespace to markup 2021-04-14 14:43:14 +09:00
Doug Gregor 9579390024 [SE-0304] Rename ConcurrentValue to Sendable 2021-03-18 22:48:20 -07:00
Doug Gregor 422fb5cd57 Move String conformance to ConcurrentValue out-of-line. 2021-02-04 13:14:24 -08:00
Doug Gregor 1a1f79c0de Introduce safety checkin for ConcurrentValue conformance.
Introduce checking of ConcurrentValue conformances:
- For structs, check that each stored property conforms to ConcurrentValue
- For enums, check that each associated value conforms to ConcurrentValue
- For classes, check that each stored property is immutable and conforms
  to ConcurrentValue

Because all of the stored properties / associated values need to be
visible for this check to work, limit ConcurrentValue conformances to
be in the same source file as the type definition.

This checking can be disabled by conforming to a new marker protocol,
UnsafeConcurrentValue, that refines ConcurrentValue.
UnsafeConcurrentValue otherwise his no specific meaning. This allows
both "I know what I'm doing" for types that manage concurrent access
themselves as well as enabling retroactive conformance, both of which
are fundamentally unsafe but also quite necessary.

The bulk of this change ended up being to the standard library, because
all conformances of standard library types to the ConcurrentValue
protocol needed to be sunk down into the standard library so they
would benefit from the checking above. There were numerous little
mistakes in the initial pass through the stsandard library types that
have now been corrected.
2021-02-04 03:45:09 -08:00
Karoy Lorentey 90d58fef8c [stdlib] Encourage more inlining on String’s UTF-8 decoding fast path 2020-12-04 02:06:21 -08:00
Erik Eckstein b42bce4ba4 stdlib: changes for the StringOptimization
To be able to constant fold string interpolation, the right semantic attributes must be in place.
Also, the interpolation's write function must be inlinable.
For the _typeName constant folding, a semantic attribute is required.
2020-07-27 21:32:56 +02:00
Mishal Shah 272c466e47 Update master to build with Xcode 12 beta 2020-06-22 15:43:20 -07:00
Valeriy Van 04e6373f28 Removes redundant buffer zeroing in lowercased() and uppercased() 2020-05-27 22:24:17 +02:00
Michael Ilseman d02f5bc509 [string] Move wCSIA check higher than _HasContiguousBytes 2020-04-10 11:39:03 -07:00
Michael Ilseman ae224cacdb [string] Restore _HasContiguousBytes for untyped storage
UnsafeRawBufferPointer cannot implement
withContiguousStorageIfAvailable because doing so would potentially
create a typed pointer from untyped data.
2020-04-09 13:38:28 -07:00
Michael Ilseman e536ad2342 [string] Outline cold path from initializer
Outline the cold, non-contiguous UTF-8 path from String(decoding:as:),
saving ~40 bytes (33%) of code size (x86_64 and arm64) from every call
site where the contiguity check cannot be constant folded away.
2020-04-09 13:38:28 -07:00
Michael Ilseman c2631004d7 [string] _HasContiguousBytes -> withContiguousStorageIfAvailable
Switch String(decoding:as) and other entry points to call
withContiguousStorageIfAvailable rather than use _HasContiguousBytes.
2020-04-09 13:38:28 -07:00
Michael Ilseman 19b332c8e2 [gardening] Delete Trailing Whitespace 2020-04-09 13:38:27 -07:00