Skip to content

Enable string conversion in EUC-JP. #1296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

YOCKOW
Copy link
Member

@YOCKOW YOCKOW commented May 16, 2025

Background: EUC-JP is not supported by OSS CoreFoundation, while it is supported by macOS Foundation Framework.
See #1016

This PR resolves the issue by calling ICU API if necessary.

@YOCKOW
Copy link
Member Author

YOCKOW commented May 16, 2025

@swift-ci Please test

@YOCKOW YOCKOW marked this pull request as ready for review May 16, 2025 09:25
@YOCKOW YOCKOW requested review from parkera and jmschonfeld May 16, 2025 09:26
// Attempt an up-call into swift-corelibs-foundation, which can defer to the CoreFoundation implementation
_cfStringEncodingConvert(string: self, using: encoding.rawValue, allowLossyConversion: allowLossyConversion) ??
// Or attempt an up-call into ICU via FoundationInternationalization
_icuStringEncodingConvert(string: self, using: encoding, allowLossyConversion: allowLossyConversion)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When do we fall back to the ICU one here? On Darwin this encoding is handled via CF, which itself calls into ICU.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that'll happen if import Foundation has not been done (and CF is present), but import FoundationInternationalization has. I wonder how we can further unify these paths; on Darwin too...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a similar question - I believe _cfStringEncodingConvert will return nil if any of the following are the case:

  1. CF isn't loaded
  2. CF is loaded but it doesn't support the provided encoding
  3. CF is loaded and supports the encoding but the data is malformed

We want the up-call in 2, and I think Tony's question was about 1, but do we want the up call in case 3?

Copy link
Member Author

@YOCKOW YOCKOW May 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I now agree that unexpected fallback should be avoided.
Although ICU can support other encodings, I should've made this change focus on EUC-JP.

}

extension ICU {
final class StringConverter: @unchecked Sendable {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: does this need the @unchecked? I would have assumed that since it's a final class with all immutable, Sendable properties that the compiler can validate this conformance

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you.
That's a vestige of my first implementation where _converter was a bare pointer...

Copy link
Member Author

@YOCKOW YOCKOW May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry.
@unchecked is necessary still because LockedState<OpaquePointer> is not Sendable...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah LockedState is Sendable but it requires its stored value to be of a Sendable type, and I had mistakenly thought OpaquePointer was Sendable but it is indeed not Sendable which is why the Sendable conformance isn't available there. This @unchecked Sendable makes sense then, since despite the pointer and anything it stores never escapes the lock.

// Attempt an up-call into swift-corelibs-foundation, which can defer to the CoreFoundation implementation
_cfStringEncodingConvert(string: self, using: encoding.rawValue, allowLossyConversion: allowLossyConversion) ??
// Or attempt an up-call into ICU via FoundationInternationalization
_icuStringEncodingConvert(string: self, using: encoding, allowLossyConversion: allowLossyConversion)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a similar question - I believe _cfStringEncodingConvert will return nil if any of the following are the case:

  1. CF isn't loaded
  2. CF is loaded but it doesn't support the provided encoding
  3. CF is loaded and supports the encoding but the data is malformed

We want the up-call in 2, and I think Tony's question was about 1, but do we want the up call in case 3?

@YOCKOW YOCKOW marked this pull request as draft May 20, 2025 03:09
YOCKOW added 2 commits May 20, 2025 12:24
Background: EUC-JP is not supported by OSS CoreFoundation, while it is supported by macOS Foundation Framework.
See swiftlang#1016

This commit resolves the issue by calling ICU API if necessary.
@YOCKOW YOCKOW force-pushed the i-see-you-legacy-string-encodings branch from 7ae3f7f to b0a8981 Compare May 20, 2025 03:45
@YOCKOW
Copy link
Member Author

YOCKOW commented May 20, 2025

@swift-ci Please test

@YOCKOW
Copy link
Member Author

YOCKOW commented May 20, 2025

@parkera @jmschonfeld Thank you for reviewing.

In response to your feedback, I've made some changes:

  • Made it clear that ICU.StringConverter doesn't support UTF-* and US-ASCII.
  • Removed unnecessary nonisolated(unsafe) from the static property.
  • Added comments in func _icuMakeStringFromBytes_impl to clarify the safety of passing the pointer.
  • Let it delegate string conversion to ICU only if the encoding is EUC-JP and there's no FOUNDATION_FRAMEWORK (at this point).

@YOCKOW YOCKOW marked this pull request as ready for review May 20, 2025 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Discrepancy: String.data(using: .japaneseEUC) returns nil on non-Darwin.
3 participants