add Foundation NSString#52
Merged
teach310 merged 2 commits intofeature/central_manager_init_optionsfrom Oct 25, 2023
Merged
Conversation
added 2 commits
October 26, 2023 00:28
## NSStringクラスを追加した理由 汎用的な文字列取得実装のため。 MicrosoftのドキュメントでStringBuilderは非推奨と書かれていたためにStringBuilderを使い続けることに抵抗があった。 https://learn.microsoft.com/ja-jp/dotnet/standard/native-interop/best-practices そして、NSDictionaryに文字列を登録するため ## NSString.HandleToStringについて 流れに関してはXamarinの実装を参考にしている。 https://github.com/xamarin/xamarin-macios/blob/main/src/CoreFoundation/CFString.cs#L190-L224 ここではFromHandleという関数名だが、NSString.FromHandleという名前であればNSStringを返した方が自然だと思うためHandleToStringという名前にした。 引数のreleaseHandleに関してはSafeNSStringHandleをusingで破棄する方が書き心地よいと感じるため不要と判断した。 ## NSStringを使用している理由 swiftのStringは値型のためUnmanaged.passRetainedの引数にすることができない。 したがって、csharp側にポインタを渡すためにはCFStringまたはNSString等のクラスを用いる必要があった。 そして、CFStringよりもNSStringのほうが扱いやすかったためNSStringを使用している。 NSStringを参照型のクラスとして使用することはswiftの公式ドキュメントにも書かれている。 > They provide reference semantics instead of value semantics, which is a useful tool to have in the toolbox. https://github.com/apple/swift-corelibs-foundation ## NSStringのcstringからのコピーをcsharp側で行っている理由 char配列を渡してそこにswiftで文字列を入れたところ文字化けしたから。 StringBuilderの場合は文字化けしないが前述の理由により避けた。 byte配列を渡せば文字化けしないかもしれないが、ArrayPoolが遅いかもしれず、最適な方法がわからなかったため 中身のコード見た感じ問題なさそうで一番楽なMarshal.PtrToStringUTF8を使用した。 https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs,80 ## LengthOfBytesUtf8について 使ってないけど、テストで活用できるため残している。
良い。NSStringを解放したらこれも解放される(内部だから)。 > This C string is a pointer to a structure inside the string object, which may have a lifetime shorter than the string object and will certainly not have a longer lifetime. https://developer.apple.com/documentation/foundation/nsstring/1411189-utf8string ## utf8StringはNSStringの中身だがこれをコピーしなくてよいのか? NSStringはヒープなため別の場所にコピーするメリットはlifetimeをNSStringと分離することだと思う。 同じでいいためしなくて良いと判断 ## cstringの文字列の長さの測り方 ここにstrlenを使った例がある https://developer.apple.com/documentation/swift/string/utf8cstring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
DictionaryのKeyで使用するNSStringを追加
汎用的な文字列返却手段としても使える
NSStringクラスを追加した理由
汎用的な文字列取得実装のため。
MicrosoftのドキュメントでStringBuilderは非推奨と書かれていたためにStringBuilderを使い続けることに抵抗があった。
https://learn.microsoft.com/ja-jp/dotnet/standard/native-interop/best-practices
そして、NSDictionaryに文字列を登録するため
NSString.HandleToStringについて
流れに関してはXamarinの実装を参考にしている。
https://github.com/xamarin/xamarin-macios/blob/main/src/CoreFoundation/CFString.cs#L190-L224
ここではFromHandleという関数名だが、NSString.FromHandleという名前であればNSStringを返した方が自然だと思うためHandleToStringという名前にした。
引数のreleaseHandleに関してはSafeNSStringHandleをusingで破棄する方が書き心地よいと感じるため不要と判断した。
NSStringを使用している理由
swiftのStringは値型のためUnmanaged.passRetainedの引数にすることができない。
したがって、csharp側にポインタを渡すためにはCFStringまたはNSString等のクラスを用いる必要があった。
そして、CFStringよりもNSStringのほうが扱いやすかったためNSStringを使用している。
NSStringを参照型のクラスとして使用することはswiftの公式ドキュメントにも書かれている。
https://github.com/apple/swift-corelibs-foundation
NSStringのcstringからのコピーをcsharp側で行っている理由
char配列を渡してそこにswiftで文字列を入れたところ文字化けしたから。
StringBuilderの場合は文字化けしないが前述の理由により避けた。
byte配列を渡せば文字化けしないかもしれないが、ArrayPoolが遅いかもしれず、最適な方法がわからなかったため
中身のコード見た感じ問題なさそうで一番楽なMarshal.PtrToStringUTF8を使用した。
https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs,80
LengthOfBytesUtf8について
使ってないけど、テストで活用できるため残している。