pygtkscintilla 自动缩进

发布于 2024-11-25 11:18:43 字数 1269 浏览 4 评论 0原文

我正在尝试翻译 C++ 代码,但我无法弄清楚“char linebuf[1000]”是什么,是否可以某种方式将其单独翻译为 python 或解释 linebuf 是什么。谢谢! :) 取自 http://www.scintilla.org/ScintillaUsage.html

if  (ch  ==  '\r'  ||  ch  ==  '\n')  {
     char  linebuf[1000];
     int  curLine  =  GetCurrentLineNumber();
     int  lineLength  =  SendEditor(SCI_LINELENGTH,  curLine);
     //Platform::DebugPrintf("[CR] %d len = %d\n", curLine, lineLength);
     if  (curLine  >  0  &&  lineLength  <=  2)  {
     int  prevLineLength  =  SendEditor(SCI_LINELENGTH,  curLine  -  1);
     if  (prevLineLength  <  sizeof(linebuf))  {
         WORD  buflen  =  sizeof(linebuf);
         memcpy(linebuf,  &buflen,  sizeof(buflen));
         SendEditor(EM_GETLINE,  curLine  -  1,
                    reinterpret_cast<LPARAM>(static_cast<char  *>(linebuf)));
         linebuf[prevLineLength]  =  '\0';
         for  (int  pos  =  0;  linebuf[pos];  pos++)  {
             if  (linebuf[pos]  !=  ' '  &&  linebuf[pos]  !=  '\t')
                 linebuf[pos]  =  '\0';
         }
         SendEditor(EM_REPLACESEL,  0,  reinterpret_cast<LPARAM>(static_cast<char  *>(linebuf)));
     }
}

I'm trying to translate the c++ code and i can't work out what "char linebuf[1000]" is, can some kind sole translate this to python or explain what linebuf is. Thanks! :) Taken from http://www.scintilla.org/ScintillaUsage.html

if  (ch  ==  '\r'  ||  ch  ==  '\n')  {
     char  linebuf[1000];
     int  curLine  =  GetCurrentLineNumber();
     int  lineLength  =  SendEditor(SCI_LINELENGTH,  curLine);
     //Platform::DebugPrintf("[CR] %d len = %d\n", curLine, lineLength);
     if  (curLine  >  0  &&  lineLength  <=  2)  {
     int  prevLineLength  =  SendEditor(SCI_LINELENGTH,  curLine  -  1);
     if  (prevLineLength  <  sizeof(linebuf))  {
         WORD  buflen  =  sizeof(linebuf);
         memcpy(linebuf,  &buflen,  sizeof(buflen));
         SendEditor(EM_GETLINE,  curLine  -  1,
                    reinterpret_cast<LPARAM>(static_cast<char  *>(linebuf)));
         linebuf[prevLineLength]  =  '\0';
         for  (int  pos  =  0;  linebuf[pos];  pos++)  {
             if  (linebuf[pos]  !=  ' '  &&  linebuf[pos]  !=  '\t')
                 linebuf[pos]  =  '\0';
         }
         SendEditor(EM_REPLACESEL,  0,  reinterpret_cast<LPARAM>(static_cast<char  *>(linebuf)));
     }
}

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

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

发布评论

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

评论(1

安稳善良 2024-12-02 11:18:43

它是一行输入文本的缓冲区,类型为 char[1000],即 1000 个 char 元素的数组(实际上是字节,因为 C++ 是基于 C 的,而 C 又早于字符编码的整个概念)。

如果我们真的想要算法的直译,Python 中最接近的可能是 array.array('B', [0]*1000)。然而,这会初始化 Python 数组,而 C++ 数组则未初始化——在 C++ 中确实没有办法跳过初始化;它只是保留空间,而不关心该内存块中已有的内容。

It is a buffer for a line of input text, of type char[1000], i.e. an array of 1000 char elements (which are actually bytes, because C++ is based upon C, which in turn predates the whole idea of character encodings).

If we really wanted a literal translation of the algorithm, the closest fit in Python is probably something like array.array('B', [0]*1000). However, this initializes the Python array, whereas the C++ array is uninitialized - there is really no way to skip that initialization in C++; it just reserves space without paying any attention to what's already in that chunk of memory.

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