635 Commits

Author SHA1 Message Date
Jake Petroules
f77a636a01 Switch to a safer technique for obtaining the working directory on Windows
Instead of looping 8 times to work around the TOCTOU issue with sizing the current directory buffer, instead keep doubling the buffer up until the 32767 character limit until the result fits. This ensures we always get a working directory if GetWorkingDirectoryW didn't return some other error, rather than returning nil in the case of a race condition.
2025-05-02 11:29:49 -07:00
Jeremy Schonfeld
7ae91602d7
upgrade AttributedStringWritingDirection SPI to API (#1275)
Signed-off-by: Jeremy Schonfeld <jschonfeld@apple.com>
Co-authored-by: Max Obermeier <m_obermeier@apple.com>
2025-04-29 11:33:01 -04:00
Jake Petroules
90953dfb68
Transparently add the \\?\ prefix to Win32 calls for extended length path handling (#1257)
On Windows, there is a built-in maximum path limitation of 260 characters under most conditions. This can be extended to 32767 characters under either of the following two conditions:

- Adding the longPathAware attribute to the executable's manifest AND enabling the LongPathsEnabled system-wide registry key or group policy.
- Ensuring fully qualified paths passed to Win32 APIs are prefixed with \?\

Unfortunately, the former is not realistic for the Swift ecosystem, since it requires developers to have awareness of this specific Windows limitation, AND set longPathAware in their apps' manifest AND expect end users of those apps to change their system configuration.

Instead, this patch transparently prefixes all eligible paths in calls to Win32 APIs with the \?\ prefix to allow them to work with paths longer than 260 characters without requiring the caller of Foundation to manually prefix the paths.

See https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation for more info.
2025-04-23 14:51:43 -07:00
Jonathan Flat
27476f9814
(149316573) Remove URL workaround for Linux crash once SCL-F is rebuilt (#1252) 2025-04-23 12:51:10 -06:00
Jonathan Flat
a842db47f0
(149532884) Revert strict IPv6 validation in URL (#1258) 2025-04-23 10:29:45 -06:00
Jeremy Schonfeld
515688303a
Make URL template regex dependency framework-only for now (#1268) 2025-04-22 13:20:20 -07:00
Ben Barham
f53d19800e
Update CMakeLists.txt with missing URLTemplate_Expression.swift (#1262)
https://github.com/swiftlang/swift-foundation/pull/1198 added most of
the new files, but missed `URLTemplate_Expression.swift`, add it.
2025-04-21 19:15:37 -07:00
Daniel Eggert
a169cebb45
URI Templating (#1198)
* Initial implementation.

* Cleanup and fix normalizedAddingPercentEncoding()

* Use failing initializer on URL instead of makeURL()

* Add standard copyright header

* Mark helper functions as fileprivate

* URL.Template.init() needs to be failable

* Rename InvalidTemplateExpression → URL.Template.InvalidExpression

* Add missing @available

* Update copyright header

* Convert tests XCTest → Swift Testing

* Use UInt8.isValidHexDigit

* Use URLParser.swift methods for unreserved + reserved characters

* Cleanup normalizedAddingPercentEncoding()

* Use String(decoding:as:)

* guard & white-space

* Cleanup “defer”

* Rename files URI… → URL…

* Add new files to CMakeLists.txt

* Add benchmarks.

* Add missing @available, 2

* Fix doc comment.

* Remove ExpressibleByStringLiteral conformance for URL.Template.VariableName

* Improve documentation comments

* Fix for  7b14d0bc62

* Fix doc comment

* Do not force unwrap “maximum length”
2025-04-21 16:01:10 -07:00
Jeremy Schonfeld
655dd5eb42
AttributedString character insertion doesn't invalidate text dependent attributes (#1256) 2025-04-21 12:05:42 -07:00
Jonathan Flat
914b9f7bbd
Support NS/CFURL re-core in Swift (#1238)
* (146349351) Support NS/CFURL re-core in Swift

* Fix .fileSystemPath() calls in Windows test

* Use encoded strings for .absoluteURL, fix NSURL bridging and CFURL lastPathComponent edge cases

* Add workaround for crash on Linux

* Fix typo
2025-04-11 22:37:51 -06:00
Jake Petroules
c16e0d9a48
Fix FileManager.isExecutableFile for emulated x86 processes on ARM systems (#1246)
GetBinaryType will return ERROR_BAD_EXE_FORMAT when querying an arm64 executable from an x86 process running on an ARM system. This change switches the implementation to use SHGetFileInfoW, which isn't subject to this quirk.

This also makes isExecutableFile behave more similarly to other platforms -- e.g. isExecutableFile already returns true for any file with the execute bit, even for an arm64 executable on an x86_64 macOS system (which it can't actually run). The spirit of the API is that the file is of an executable type, not necessarily that the running system is capable of executing it.

The practical consequence of fixing this bug is that queries like:

```swift
FileManager.default.isExecutableFile(atPath: "C:\\Windows\\system32\\cmd.exe")
```

will now correctly return true regardless of what architecture the binary is compiled for or what type of system it's running on.

Closes #860
2025-04-11 09:12:53 -07:00
Max Obermeier
78a0395efa
Writing Direction Attribute Implementation (#1245)
* implementation

* address review
2025-04-10 17:17:57 -07:00
Jeremy Schonfeld
201a45b0aa
Lazily compute whether attribute changed in _attributeModified(_:old:new:) (#1237) 2025-04-07 15:06:58 -07:00
3405691582
03fe46f43b
OpenBSD support. (#1126)
* Advise porter on where to make necessary change.

In #1075 the change was already made for BSD (thank you!); my working
edit had this guidance to ensure future porters get an error directing
them where to make a necessary change.

Otherwise, the FoundationEssentials build will fail and complain these
variables are not defined but not have guidance as to where they are
sourced from.

* OpenBSD does not support extended attributes.

* OpenBSD does not have secure_getenv.

* Remaining OpenBSD changes.

* OpenBSD also needs `pthread_mutex_t?`.

* Originally I followed Darwin's check with `d_namlen`, but this should
  work too.

* Correct statvfs type casts for OpenBSD.

On OpenBSD, fsblkcnt_t -- the type of f_blocks -- is a UInt64; therefore,
so must `blockSize` be.

Ultimately, both sides of the `totalSizeBytes` multiplication should
probably be type cast for all platforms, but that's a more significant
functional change for another time.

* Default activeProcessorCount to 1, not 0.

After a rather tedious debugging session trying to figure out why
swiftpm-bootstrap appeared to be deadlocked, this turned out to be the
culprit. Perhaps this should be #error instead, but for now, set a
sensible default.

* Use sysconf for activeProcessorCount.

This is what Dispatch does in some places for OpenBSD anyway, so do
likewise here.
2025-04-07 10:46:18 -07:00
Yuta Saito
769081929d
build: Fix CMake build by adding missing source file (#1239)
Follow-up to 63cfcb9bfde8153a9f385f60ee437fc45912a1dc
2025-04-04 21:04:35 -07:00
Tony Parker
63cfcb9bfd
ISO8601 DateComponents format style (#1209)
* ISO8601 DateComponents style

* Add Hashable to ISO8601FormatStyle
2025-04-04 13:58:57 -07:00
Yuta Saito
320c5837e2
[wasm] Add O_NONBLOCK shim (#1235)
05975959e67f57063445baabb652262dd764cc72 added a use of `O_NONBLOCK` but
the constant cannot be imported from wasi-libc through ClangImporter
directly, so we need to add a shim for it as well as other constants.
2025-03-31 08:54:37 -07:00
David Smith
b0b89ccac1
Allocationless constant String -> NSString bridging via a new tagged pointer type (#1232)
* Revert "Revert "rdar://142693100 (Allocationless constant String -> NSString bridging via a new tagged pointer type) (#2798)" (#2843)"

This reverts commit c378439322f5b960ecb972b27ab1419a622ad6b2.

* Adopt the new entry point for bridging the new tagged pointers

* All remaining callsites passed null-terminated strings, so just eliminate the isTerminated bit and simplify further. Also gets us up to 13 bits of length

* Address review comments
2025-03-28 10:19:09 -07:00
michael-yuji
7abeeef0f2
fix a typo preventing freebsd to build (#1231) 2025-03-28 09:54:42 -07:00
Jeremy Schonfeld
b72fb0ff84
Improve performance of enumerating constraint-bound attributes consistent across many runs (#1226) 2025-03-28 09:36:57 -07:00
Tony Parker
8c87e9eaac
Use the default time zone instead of gmt, to match the previous behavior of ISO8601 calendar (#1229) 2025-03-27 15:35:22 -07:00
Tony Parker
05975959e6
Use nonblocking IO for open (#1228)
Windows is blocked by a CI/infra issue. This doesn't affect that code, so merging.
2025-03-27 12:57:48 -07:00
Kenta Kubo
68dd6d85a6
[wasm] Make FileManager.createFile() work on WASI (#992)
* [wasm] Make `FileManager.createFile()` work on WASI

fixes swiftwasm/swift#5593

`FileManager.createFile()` currently doesn't work on WASI because it
requires `.atomic`, it requires creating a temporary file, and it isn't
suppported on WASI.

So I have fixed that by removing the `.atomic` requirement only on WASI.

* [wasm] Make `Data.WritingOptions.atomic` unavailable on WASI

`writeToFileAux`, `createTemporaryFile`, and `createProtectedTemporaryFile` also become unavailable on WASI.
2025-03-25 09:42:15 -07:00
Jeremy Schonfeld
95a222fa15
Improve performance for comparing AttributedStrings with differing character counts (#1224)
* Improve performance for comparing AttributedStrings with differing character counts

* Address feedback

* Fix typo
2025-03-24 16:04:14 -07:00
Tony Parker
ee467a19cb
Add an upcall point to swift-corelibs-foundation for String encoding conversion (#1217)
* Add an upcall point to swift-corelibs-foundation for String encoding conversion

* Add upcall for conversion from bytes to String in non-swift-foundation encodings
2025-03-24 09:28:00 -07:00
kperryua
3179457ef5
rdar://138657153 (Writing NSData atomically defaults to using noFileProtection (class D)) (#1223) 2025-03-21 15:13:46 -07:00
Jeremy Schonfeld
9ba455dcfa
Fix ISO Latin 1 Encoding/Decoding issues (#1219) 2025-03-19 14:24:02 -07:00
Christopher Thielen
fa43c96f5f
Move temporarily relocated FileManager extensions back to their original locations (#1213) 2025-03-18 11:29:53 -07:00
michael-yuji
2f65d91455
Fix _copyDirectoryMetadata compilation error on FreeBSD (#1121)
* Fix FreeBSD build

* address comments
2025-03-17 14:00:41 -07:00
Tony Parker
a95c2c8d43
Clean up some NO_FILESYSTEM paths (#1210) 2025-03-13 12:50:39 -07:00
Tony Parker
2ea76c322f
Add an HTTP format style for both Date and DateComponents (#1127)
* Add an HTTP format style for both `Date` and `DateComponents`.

* Correct the comments to properly use DateComponents type

* Use default values instead of crashing when formatting date components with missing or invalid fields

* Use 59 seconds instead of 0 for leap second of 60 - this results in being off by only 1 second in this case instead of 59.

* Address some review feedback
2025-03-13 11:39:57 -07:00
Jeremy Schonfeld
4c021b6e57
Make trivial AttributedString dynamicMemberLookup subscripts inlinable (#1207) 2025-03-12 12:43:14 -07:00
Tina L
6cde050fb3
Introduce compatibility behavior of Calendar.date(byAdding:value:to:wrappingComponents) (#1206)
Some app is passing in `value: -9223372036854775808` to the function below, and force unwrapping the result. It then crashes when the force unwrap fails.

```swift
/// - returns: A new date, or nil if a date could not be calculated with the given input.
public func date(byAdding component: Component, value: Int, to date: Date, wrappingComponents: Bool = false) -> Date?
```

This is caused by #1149. Prior to the change, we were using `_CalendarICU`'s implementation, where we truncate the input value to `Int32`, which becomes 0 in this case. That results in us returning the input `date` unchanged.

Now with #1149, we truthfully return `nil` because the calculation cannot be done.

We could restore the old behavior, but that implementation is incorrect. It's also clearly a wrong assumption on the client side. Add a compatibility check and restore the old behavior if needed.

Resolves 145862455
2025-03-12 09:12:30 -07:00
Tina L
3c9559d68a
Revert #1160 (#1205)
Revert https://github.com/swiftlang/swift-foundation/pull/1160 because it caused some app to fail to launch.
2025-03-11 11:59:44 -07:00
Jonathan Flat
f751db3371
Use lstat() semantics for URL directory detection (#1204)
* (145233243) Use lstat() semantics for URL directory detection

* Fix Windows try?, fix symlink path in test

* Check for reparse point flag explicitly

* Move path.isEmpty check outside
2025-03-07 16:15:51 -07:00
करन मिश्र
be44158fd9
Remove Thai Calendar (#1203)
145890794
2025-03-06 16:24:25 -08:00
Jeremy Schonfeld
93e3bac3d9
(144909030) FSR writes bytes to end of buffer even on success cases (#1199) 2025-03-04 21:54:29 -08:00
Johannes Weiss
601e847faa
always @preconcurrency import Glibc (#1175)
Co-authored-by: Johannes Weiss <johannes@jweiss.io>
2025-03-04 09:25:51 -08:00
Jeremy Schonfeld
bfcba67731
AttributedString Index Tracking (#1109)
This implements tracking of AttributedString indices across mutations
2025-02-28 16:15:42 -08:00
Jeremy Schonfeld
de6af532ad
AttributedString Index Validity APIs (#1177)
* AttributedString index validity APIs

* Move canImport(Synchronization) into function body

* Remove unnecessary bounds checks from validity expression

* Update FOUNDATION_FRAMEWORK code to provide Index versions
2025-02-28 12:17:21 -08:00
Jeremy Schonfeld
c2d30f4ab6
AttributeContainer Filtering (#1182) 2025-02-28 09:36:05 -08:00
Fabian Fett
d1ff55846e
Base64 faster decode (#1160)
* Make decoding go fast!

* Fixes

* Use typed throws everywhere to prevent allocation in the unhappy path.

* cleanup

* More cleanup

* Fix tests

* Removed Chromium callout in function name
2025-02-25 09:37:33 -08:00
Fabian Fett
b98db399ef
[Base64] Fix: Dont add line separators in last line when last line fits (#1184) 2025-02-21 12:34:14 -08:00
Fabian Fett
2c08d60255
Base64 faster encode (#1157)
* Make Base64 encoding faster

* Add NOTICE.txt

* Add swift-extras-base64 to NOTICE.txt

* Rename internal functions

* Move licenses around

* Fixes

* Fix Swift 6.0
2025-02-20 09:33:54 -08:00
kperryua
8a7adf2bde
New XML PropertyListDecoder parser accepts malformed tags (#1180) 2025-02-18 12:59:29 -08:00
करन मिश्र
47e587a9a4
New Calendars (#1171)
Exposed 12 new calendar identifiers
2025-02-17 14:14:27 -08:00
Jeremy Schonfeld
868f888c41
(113200224) JSON/PropertyList coders userInfo should require Sendable values (#1169) 2025-02-17 13:26:41 -08:00
Jeremy Schonfeld
3b31f75300
(140308690) AttributedString APIs for operations over noncontiguous ranges (#1145)
This PR implements the API changes proposed in SF-0014 by adding a new DiscontiguousAttributedSubstring type (the result of slicing an AttributedString with a RangeSet) which vends out slices of each underlying view, an upgraded Runs view which now handles discontiguous segments, and performs operations over the discontiguous subranges.
2025-02-17 10:48:14 -08:00
Jeremy Schonfeld
7aacaff577
Drop UTF-8 BOM when present while decoding UTF-8 bytes into String (#1165) 2025-02-13 15:52:30 -08:00
Allan Shortlidge
06ccc4ced6
Revert "New Calendar Identifiers (#1168)" (#1170)
This reverts commit cc6084b366ca29d0fe06f1ac6060d22c45ab876d.
2025-02-13 14:25:55 -08:00