1

我正在尝试翻译 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)));
     }
}
4

1 回答 1

0

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

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

于 2011-07-20T22:03:03.703 回答