Commit Graph

64 Commits

Author SHA1 Message Date
Guillaume Lessard
dfb2e2f12e [stdlib] annotate uses of Range.init(_uncheckedBounds:) 2025-03-05 18:52:11 -08:00
Doug Gregor
22eecacc35 Adopt unsafe annotations throughout the standard library 2025-02-26 14:28:01 -08:00
Guillaume Lessard
8912098c01 [doc] adjust to reflect the current implementation. 2023-07-12 14:13:19 -07:00
Valeriy Van
95d25d3ba5 Minor comment typo fix 2022-11-15 10:40:43 +02:00
Karoy Lorentey
cb2194c024 [stdlib] Fix ABI and portability issues 2022-04-13 19:15:30 -07:00
Karoy Lorentey
b33fefb71c [stdlib] String: be more consistent about when markEncoding is called 2022-04-13 18:38:41 -07:00
Karoy Lorentey
67adcabefc Apply notes from code review 2022-04-10 00:14:43 -07:00
Karoy Lorentey
3c9968945e [stdlib] String: Implement happy paths for index validation 2022-04-10 00:14:43 -07:00
Karoy Lorentey
d24ae9dfcd [stdlib] Remove Substring._endIsCharacterAligned
Now that the cached character stride in indices always mean the
stride in the full string, we can stop looking at whether a substring
has a character-aligned end index.
2022-04-09 21:33:53 -07:00
Karoy Lorentey
83df814c63 [stdlib] _StringObject.isKnownUTF16 → isForeignUTF8
This fixes a compatibility issue with potential future UTF-8 encoded
foreign String forms, as well as simplifying the code a bit — we no
longer need to do an availability check on inlinable fast paths.

The isForeignUTF8 bit is never set by any past or current stdlib
version, but it allows us to introduce UTF-8 encoded foreign forms
without breaking inlinable index encoding validation introduced in
Swift 5.7.
2022-04-09 21:33:53 -07:00
Karoy Lorentey
73312fedd4 [stdlib] Grapheme breaking: Refactor to simplify logic
- Split forward and backward direction into separate code paths.
  This makes the code more readable and paves the way for future
  improvements. (E.g., switching to a linear-time algorithm for
  breaking backwards.)
- `Substring.index(after:)` now uses the same grapheme breaking paths
  as `String.index(after:)`.
- The cached stride value in string indices is now well-defined even
  on indices that aren’t character-aligned.
2022-04-05 20:47:42 -07:00
Karoy Lorentey
e0bd5f7a79 [stdlib] Fix Substring.UnicodeScalarView.replaceSubrange 2022-04-05 20:47:42 -07:00
Karoy Lorentey
2e9fd9eb6b [stdlib] Substring.UnicodeScalarView: Add _invariantCheck 2022-04-05 20:47:42 -07:00
Karoy Lorentey
f7c674ed55 [stdlib] Slice._bounds, Range<String.Index>._encodedOffsetRange: New helpers 2022-04-05 20:47:42 -07:00
Karoy Lorentey
ff58d54565 [stdlib][NFC] Substring adjustments 2022-03-29 20:10:40 -07:00
Karoy Lorentey
c9adf7aaea [stdlib] Substring: Review view creation/conversion code 2022-03-29 20:10:40 -07:00
Karoy Lorentey
b7c54ac41c [stdlib] Substring.init: Stop checking things twice 2022-03-29 20:00:08 -07:00
Karoy Lorentey
9714f97ad8 [stdlib] Substring: round indices down to nearest character in indexing operations
Distances between indices aren’t well-defined without this.
2022-03-29 20:00:08 -07:00
Karoy Lorentey
4eab8355ca [stdlib] String: prefer passing ranges to start+end argument pairs 2022-03-29 20:00:08 -07:00
Karoy Lorentey
4ad8b26ab3 [stdlib] String.UTF16View: Review/fix index validation
Also, in UTF-16 slices, forward collection methods to the base view
instead of `Slice`, to make behavior a bit easier to understand.

(There is no need to force readers to page in `Slice`
implementations _in addition to_ whatever the base view is doing.)
2022-03-29 20:00:08 -07:00
Karoy Lorentey
5f6c300adb [stdlib] String.UTF8View: Review/fix index validation
Also, in UTF-8 slices, forward collection methods to the base view
instead of `Slice`, to make behavior a bit easier to understand.

(There is no need to force readers to page in `Slice`
implementations _in addition to_ whatever the base view is doing.)
2022-03-29 18:40:25 -07:00
Karoy Lorentey
d58811262d [stdlib] String.UnicodeScalarView: Review index validation 2022-03-29 18:40:25 -07:00
Karoy Lorentey
0523b67e1f [stdlib] String.index(_:offsetBy:limitedBy:): compare limit against original index
Whether the limit actually applies depends on how it’s ordered
relative to the original index `i`, not the one we round down to the
nearest Character.
2022-03-24 21:00:00 -07:00
Karoy Lorentey
c436654b61 [stdlib] Substring._characterStride(startingAt:): Limit stride to the correct bounds 2022-03-24 21:00:00 -07:00
Karoy Lorentey
6d400c81a2 [stdlib] Substring: remove _encodedOffsetRange in favor of existing _offsetRange 2022-03-24 21:00:00 -07:00
Karoy Lorentey
2464aa681e [stdlib] String: Ensure indices are marked scalar aligned before rounding down to Character 2022-03-24 21:00:00 -07:00
Karoy Lorentey
321284e9a9 [stdlib] Review & fix index validation during String index conversions
- Validate that the index has the same encoding as the string
- Validate that the index is within bounds
2022-03-24 21:00:00 -07:00
Karoy Lorentey
6245da2457 [stdlib] Substring: Be consistent about how we refer to the underlying string
Prefer direct stored properties to computed ones — there is no reason
to risk inlining issues, esp. since things like `Slice.base` aren’t
even force-inlined.

Prefer using `_wholeGuts` to spelling out the full incantation.
2022-03-24 21:00:00 -07:00
Karoy Lorentey
8ab2379946 [stdlib] Round indices down to nearest Character in String’s index algorithms
To prevent unaligned indices from breaking well-defined index distance
and index offset calculations, round every index down to the nearest
whole Character.

For the horrific details, see the forum discussion below.

https://forums.swift.org/t/string-index-unification-vs-bidirectionalcollection-requirements/55946

To avoid rounding from regressing String performance in the regular
case (when indices aren’t being passed across string views), introduce
a new String.Index flag bit that indicates that the index is already
Character aligned.
2022-03-24 21:00:00 -07:00
Karoy Lorentey
87073f2af8 [stdlib] Substring.replaceSubrange: fix startIndex/endIndex adjustment
This used to forward to `Slice.replaceSubrange`, but that’s a generic algorithm that isn’t aware of the pecularities of Unicode extended grapheme clusters, and it can be mislead by unusual cases, like a substring or subrange whose bounds aren’t `Character`-aligned, or a replacement string that starts with a continuation scalar.
2022-03-24 21:00:00 -07:00
Karoy Lorentey
a44997eeea [stdlib] Factor scalar-aligned String index validation out into a set of common routines
There are three flavors, corresponding to i < endIndex, i <= endIndex, and range containment checks.
Additionally, we have separate variants for index validation in substrings.
2022-03-24 21:00:00 -07:00
Karoy Lorentey
15c7721caf [stdlib] Use the new index encoding flags when marking the encoding of indices
This removes an unnecessary opaque call from the inlinable path, but it preserves a runtime version check.
2022-03-24 20:59:59 -07:00
Karoy Lorentey
6e18955f90 [stdlib] Add bookkeeping to keep track of the encoding of strings and indices
Assign some previously reserved bits in String.Index and _StringObject to keep track of their associated storage encoding (either UTF-8 or UTF-16).

None of these bits will be reliably set in processes that load binaries compiled with older stdlib releases, but when they do end up getting set, we can use them opportunistically to more reliably detect cases where an index is applied on a string with a mismatching encoding.

As more and more code gets recompiled with 5.7+, the stdlib will gradually become able to detect such issues with complete accuracy.

Code that misuses indices this way was always considered broken; however, String wasn’t able to reliably detect these runtime errors before. Therefore, I expect there is a large amount of broken code out there that keeps using bridged Cocoa String indices (UTF-16) after a mutation turns them into native UTF-8 strings. Therefore, instead of trapping, this commit silently corrects the issue, transcoding the offsets into the correct encoding.

It would probably be a good idea to also emit a runtime warning in addition to recovering from the error. This would generate some noise that would gently nudge folks to fix their code.

rdar://89369680
2022-03-24 20:59:59 -07:00
Karoy Lorentey
683b9fa021 [stdlib] Adjust/fix String’s indexing operations to deal with the consequences of SE-0180 2022-03-24 20:59:59 -07:00
Richard Wei
e7e9e06de6 [Doc] Fix unescaped reference in 'Substring.base' summary.
There is an unescaped reference to type `Substring`.  I think a plain lowercased reference would read better.  Alternatively we could escape it with backquotes.
2021-11-18 16:49:44 -08:00
Karoy Lorentey
dd40ff2929 [stdlib][NFC] Put stored properties & primary initializers before other members in struct declarations 2021-10-05 22:01:35 -07:00
Kuba (Brecka) Mracek
404badb49a Introduce SWIFT_ENABLE_REFLECTION to turn on/off the support for Mirrors and reflection (#33617) 2021-09-08 13:08:13 -07:00
Doug Gregor
9579390024 [SE-0304] Rename ConcurrentValue to Sendable 2021-03-18 22:48:20 -07: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
93368213cf [stdlib] Don’t use unbound ranges (foo[…]) in String → Substring conversions
The UnboundRange → Range conversion path is complicated and it involves at least one unnecessary _precondition check for startIndex ..< endIndex.
2020-12-04 19:55:39 -08:00
Karoy Lorentey
513a7d9e19 [stdlib] Substring: Restore original Range paths
This eliminates a couple of _debugPreconditions which seem to change inlining enough to interfere with some benchmarks.
2020-12-04 18:35:08 -08:00
Erik Eckstein
0102fe8b15 stdlib: remove some inlinable annotations from Substring's and String's subscript
Inlining those operations does not really give any benefit.
Those operations are complex and most likely, cannot be folded into simpler patterns after inlining, anyway.
Reduces some code size for cases where those functions were inlined (due to a not perfect inlining heuristics).
2020-07-16 12:46:42 +02:00
Michael Ilseman
38fce16254 [string] Convert IR tests to SIL tests 2020-04-10 11:39:22 -07:00
Michael Ilseman
2897daa434 Merge pull request #22616 from karwa/substring_inline
[stdlib] Make some trivial Substring methods inlinable
2020-02-20 09:25:38 -08:00
Cory Benfield
c6dfea6fc4 [stdlib] Re-add withContiguousStorageIfAvailable to SubString.UTF8View
This is a second pass at the original patch, which broke an OS test.

Due to an oversight it seems that we never added a
withContigousStorageIfAvailable implementation to SubString.UTF8View,
which meant that if you sliced a String you lost the ability to get fast
access to the backing storage. There's no good reason for this
functionality to be missing, so this patch adds it in by delegating to
the Slice implementation.

Resolves SR-11999.
2020-01-14 14:01:06 +00:00
Michael Ilseman
70e6db81f1 Revert "[stdlib] Add withContiguousStorageIfAvailable to SubString.UTF8View" 2020-01-10 13:28:03 -08:00
Cory Benfield
68f0816daa [stdlib] Add withContiguousStorageIfAvailable to SubString.UTF8View
Due to an oversight it seems that we never added a
withContigousStorageIfAvailable implementation to SubString.UTF8View,
which meant that if you sliced a String you lost the ability to get fast
access to the backing storage. There's no good reason for this
functionality to be missing, so this patch adds it in by delegating to
the Slice implementation.

Resolves SR-11999.
2020-01-10 09:10:09 +00:00
Paul Hudson
06f82a53b5 Replaced the majority of ' : ' with ': '. 2019-07-18 20:46:07 +01:00
Michael Ilseman
4cd1e812b7 [String] Scalar-alignment bug fixes.
Fixes a general category (pun intended) of scalar-alignment bugs
surrounding exchanging non-scalar-aligned indices between views and
for slicing.

SE-0180 unifies the Index type of String and all its views and allows
non-scalar-aligned indices to be used across views. In order to
guarantee behavior, we often have to check and perform scalar
alignment. To speed up these checks, we allocate a bit denoting
known-to-be-aligned, so that the alignment check can skip the
load. The below shows what views need to check for alignment before
they can operate, and whether the indices they produce are aligned.

┌───────────────╥────────────────────┬──────────────────────────┐
│ View          ║ Requires Alignment │ Produces Aligned Indices │
╞═══════════════╬════════════════════╪══════════════════════════╡
│ Native UTF8   ║ no                 │ no                       │
├───────────────╫────────────────────┼──────────────────────────┤
│ Native UTF16  ║ yes                │ no                       │
╞═══════════════╬════════════════════╪══════════════════════════╡
│ Foreign UTF8  ║ yes                │ no                       │
├───────────────╫────────────────────┼──────────────────────────┤
│ Foreign UTF16 ║ no                 │ no                       │
╞═══════════════╬════════════════════╪══════════════════════════╡
│ UnicodeScalar ║ yes                │ yes                      │
├───────────────╫────────────────────┼──────────────────────────┤
│ Character     ║ yes                │ yes                      │
└───────────────╨────────────────────┴──────────────────────────┘

The "requires alignment" applies to any operation taking a
String.Index that's not defined entirely in terms of other operations
taking a String.Index. These include:

* index(after:)
* index(before:)
* subscript
* distance(from:to:) (since `to` is compared against directly)
* UTF16View._nativeGetOffset(for:)
2019-06-26 16:42:58 -07:00
Ben Cohen
e9d4687e31 De-underscore @frozen, apply it to structs (#24185)
* De-underscore @frozen for enums

* Add @frozen for structs, deprecate @_fixed_layout for them

* Switch usage from _fixed_layout to frozen
2019-05-30 17:55:37 -07:00