为什么有基于 1 的行号和基于 0 的字符号的约定?

发布于 2024-08-16 15:16:04 字数 279 浏览 2 评论 0原文

根据 TkDocs

这里的“1.0”代表插入文本的位置,可以读作“第1行,第0个字符”。这是指第一行的第一个字符;对于与程序员通常如何引用行和字符相关的历史约定,行号是从 1 开始的,字符号是从 0 开始的。

我以前没有听说过这个大会,在谷歌上也找不到任何相关信息。有人可以向我解释一下吗?

According to TkDocs:

The "1.0" here represents where to insert the text, and can be read as "line 1, character 0". This refers to the first character of the first line; for historical conventions related to how programmers normally refer to lines and characters, line numbers are 1-based, and character numbers are 0-based.

I hadn't heard of this convention before, and I can't find anything relevant on Google. Can anyone explain this to me please?

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

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

发布评论

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

评论(2

南风几经秋 2024-08-23 15:16:04

我认为你指的是 Tk 的文本小部件。手册页说:

行从 1 开始编号,以便与使用此编号方案的其他 UNIX 程序保持一致。

不过,我不确定它指的是哪些 Unix 工具。

更新:
正如评论中提到的,看起来很多 unix 文本操作工具从 1 开始行编号。而 tcl/tk 具有 unix 起源,因此尽可能与底层操作系统环境兼容是有意义的。

I think you're referring to Tk's text widget. The man page says:

Lines are numbered from 1 for consistency with other UNIX programs that use this numbering scheme.

Although, I'm not sure which Unix tools it's talking about.

Update:
As mentioned in the comments, it looks like a lot of unix text manipulation tool starts line numbering at 1. And tcl/tk having a unix origin, it makes sense to be as compatible as possible with the underlying OS environment.

潦草背影 2024-08-23 15:16:04

这确实只是惯例,但这里有一个建议。

通常认为字符位置与 Java 迭代器相同,它是指向两个字符之间位置的“指针”。因此,第一个字符是索引位置 0 之后的字符。例如,子字符串是在两个字符间位置之间获取的。

另一方面,行位置通常更多地被认为是 .NET 枚举器的方式,它是指向项目本身的“指针”,而不是指向两者之间的位置。因此第一行是位置 1 处的行。

It really is nothing more than convention, but here is a suggestion.

Character positions are generally thought of in the same way as a Java iterator, which is a "pointer" to a position between two characters. Thus the first character is the one after index position 0. Substrings are taken between two inter-character positions, for instance.

Line positions on the other hand are generally thought of more in the way of a .NET enumerator, which is a "pointer" to the item itself, not to a position in between. Thus the first line is the line at position 1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文