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 1 commit 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.

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
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.

internal import _FoundationICU

private extension String.Encoding {
var _icuConverterName: String? {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we make sure this returns nil for any encoding which we never want to use ICU for? UTF8, ASCII, UTF16/32, etc.

Copy link
Member Author

Choose a reason for hiding this comment

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

Certainly.
I'll let them return nil here or in ICU.StringConverter's init.

}

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...

}

extension ICU.StringConverter {
nonisolated(unsafe) static private var _converters: LockedState<[String.Encoding: ICU.StringConverter]> = .init(initialState: [:])
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should just be a static private let (the immutable-ness affects the LockedState itself but not the contents IIRC, and this isn't unsafe because it's guarded by a lock)

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed it should be.
This is also a vestige of my first impl, I guess.

return nil
}
let data = Data(
bytesNoCopy: UnsafeMutableRawPointer(mutating: pointer),
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it worth adding a comment here about this being "safe" because this data neither escapes this function nor is mutated? Since the pointer provided is not guaranteed to live longer than the function or be mutable, this could be a trap for a later addition that doesn't see those two constraints

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree it's better to have some comments. I'm going to add ones.

// 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?

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