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)));
}
}
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.
精彩评论