Skip to content

cstrings: add NSStringToString#447

Merged
TotallyGamerJet merged 8 commits into
ebitengine:mainfrom
TotallyGamerJet:#438_nsstring
May 9, 2026
Merged

cstrings: add NSStringToString#447
TotallyGamerJet merged 8 commits into
ebitengine:mainfrom
TotallyGamerJet:#438_nsstring

Conversation

@TotallyGamerJet
Copy link
Copy Markdown
Collaborator

What issue is this addressing?

Closes #438

What type of issue is this addressing?

feature

What this PR does | solves

Add function NSStringToString to convert an NSString ID to a Go string.

I put this in a new package cstrings so as to not create a dependency on Foundation framework in the objc package. This package would also be a good spot to export the internal strings.GoString function.

@TotallyGamerJet TotallyGamerJet changed the title nsstrings: add NSStringToString cstrings: add NSStringToString May 9, 2026
Comment thread cstrings/nsstrings_darwin.go
Comment thread cstrings/nsstrings_darwin_test.go
Comment thread cstrings/nsstrings_darwin.go Outdated
@hajimehoshi
Copy link
Copy Markdown
Member

I put this in a new package cstrings so as to not create a dependency on Foundation framework in the objc package.

I understand why we need a separate package. Do we have a plan to put other things than objc helpers?

If we need helpers for the objc package, what about objcutil?

@hajimehoshi
Copy link
Copy Markdown
Member

Another safer bet is puregoutil.

Comment thread cstrings/nsstrings_darwin.go Outdated
Comment thread cstrings/nsstrings_darwin.go Outdated
Comment thread cstrings/nsstrings_darwin.go Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a Darwin-only helper to convert Objective-C NSString objects (passed as objc.ID) into Go string, addressing the common footgun around UTF-8 vs UTF-16 length handling (Issue #438).

Changes:

  • Introduces new cstrings package with NSStringToString(objc.ID) string on Darwin.
  • Loads Foundation at init-time to ensure NSString/related classes are available.
  • Adds Darwin-only tests for nil input, empty string, normal string, and non-NSString panic behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
cstrings/nsstrings_darwin.go Adds NSStringToString implementation and init-time Foundation/selector/class setup.
cstrings/nsstrings_darwin_test.go Adds unit tests validating conversion behavior and panic on non-NSString input.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cstrings/nsstrings_darwin.go
Comment thread cstrings/nsstrings_darwin_test.go
Comment thread cstrings/nsstrings_darwin_test.go
Copy link
Copy Markdown
Member

@hajimehoshi hajimehoshi left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@hajimehoshi
Copy link
Copy Markdown
Member

Still LGTM

@TotallyGamerJet TotallyGamerJet merged commit f8849c8 into ebitengine:main May 9, 2026
42 checks passed
@TotallyGamerJet TotallyGamerJet deleted the #438_nsstring branch May 9, 2026 18:59
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.

objc: Add function to convert NSString to Go string

3 participants