进程内存中的 Unicode 字符串

发布于 2024-07-10 23:25:26 字数 193 浏览 5 评论 0原文

在处理 unicode 字符串时,内存中最优选的格式是什么? 为什么?

我正在通过为其生成可执行文件映像来实现一种编程语言。 显然,工作的编程语言实现需要一个处理字符串的协议。

我考虑过使用动态数组作为字符串的基础,因为它们实现起来非常简单,并且对于短字符串非常有效。 我只是不知道以这种方式使用字符串时字符的最佳格式。

What is the most preferred format of unicode strings in memory when they are being processed? And why?

I am implementing a programming language by producing an executable file image for it. Obviously a working programming language implementation requires a protocol for processing strings.

I've thought about using dynamic arrays as the basis for strings because they are very simple to implement and very efficient for short strings. I just have no idea about the best possible format for characters when using strings in this manner.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幽蝶幻影 2024-07-17 23:25:26

UTF16 是使用最广泛的格式。

UTF16 相对于 UTF8 的特点是,尽管不太紧凑,但每个字符都有 2 字节(16 位)的恒定大小 - 只要您不使用代理(当坚持 2 字节字符时,编码称为 UCS-2)。

在 UTF8 中,只有一小部分字符是用 1 个字节编码的,其他字符则用 4 个字节编码。 这使得字符处理不那么直接并且更容易出错。

当然,首选使用 Unicode,因为它可以处理国际字符。

UTF16 is the most widely used format.

The advantage of UTF16 over UTF8 is that, despite being less compact, every character has a constant size of 2bytes (16bits) - as long as you don't use surrogates (when sticking to 2bytes chars, the encoding is called UCS-2).

In UTF8 there is only a small set of characters coded on 1bytes, others are up 4 bytes. This makes character processing less direct and more error prone.

Of course using Unicode is preferred since it enables to handle international characters.

却一份温柔 2024-07-17 23:25:26

C Python 2.x 系列使用 UTF-16 和 UCS-4,具体取决于平台/构建/等。

这里有一个来自 python-dev 的有趣讨论选择 Python 3.0 的 Unicode 内部表示时的要求和权衡。 虽然其中的内容比我可以简要描述的要多,但它包括:

  • 讨论外部接口(恒定时间切片、.lower、.islower 等的高效实现)
  • 外部需求(GTK 接受 UTF-8 字符串,QT 接受 UTF-16)和 UCS-4 字符串,Windows 采用 UT-16 字符串等)
  • 它指向 Unicode 数据的其他实现(例如 QT)。
  • 它讨论了重要的用例(与外部接口密切相关)。
  • ETC。

The C Python 2.x series used UTF-16 and UCS-4 depending on platform/build/etc.

Here's an interesting discussion from python-dev on the requirements and trade-offs in choosing the Unicode internal representation for Python 3.0. While there's more content there than I can briefly describe, it includes:

  • Discussing the external interface (constant time slicing, efficient implementations of .lower, .islower, etc.)
  • External requirements (GTK takes UTF-8 strings, QT takes UTF-16 and UCS-4 strings, Windows takes UT-16 strings, etc.)
  • It points at other implementations of Unicode data (eg. QT's).
  • It discusses important use cases (which is closely related to external interface).
  • etc.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文