gpt4 book ai didi

c++ - 在给定列和行的字符数组中查找索引?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:34:33 25 4
gpt4 key购买 nike

这是我的情况。

我有一个自动换行算法,当宽度太大或在文本中找到\n 或 '-' 时,它会生成文本行。

因此它不会将 '\n' 包含在文本行本身中。这对我来说是具有挑战性的部分。

我正在尝试制作一个算法,在给定一行和一列的情况下返回字符数组中的索引。

所以如果第 1 行的长度是 20,第 2 行的长度是 10,如果第 1 行最初有一个 '\n',它应该考虑到这一点。

例如:

正文是:

Hello blue sky\nworld

然后我们得到 2 行:

Hello blue sky
world

现在如果我的插入符由“|”表示我把它放在这里:

  Hello blue sky
|world

结果不是第 14 个字符,而是第 15 个字符,因为必须考虑不可见的 '\n'。

棘手的部分是如果自动换行决定生成一个 '\n'

例如:

  Hello blue 
sky
|world

在这种情况下,结果应该仍然是 15,因为自动换行推了一个新行,而不是因为文本中有 '\n'。

谢谢

Here is my mess so far:

int AguiTextBox::indexFromColumnRow( int column, int row )
{
int len = 0;
int charCount = 0;
std::string curChar;

int curLen = 0;
int bytesSkipped = 0;
std::string::const_iterator it = getText().begin();
std::string::const_iterator end = getText().end();

if(textRows.size() == 0)
{
return -1;
}
for(int i = 0; i < row; ++i)
{
len = _unicodeFunctions.getUtf8StringLength(textRows[i]);

for(int j = 0; j < len; ++j)
{
//skip characters that would not have passed the newline test
do
{
curLen = _unicodeFunctions.bringToNextUnichar(it,end);
curChar = getText().substr(bytesSkipped,curLen);
bytesSkipped += curLen;
charCount++;
}
while(curChar[0] < ' ');
}

}

len = len = _unicodeFunctions.getUtf8StringLength(textRows[row]);


if(column == 0 && charCount + 1 < getTextLength())
{
curChar = _unicodeFunctions.getUtf8SubStr(getText(),charCount,1);

while(charCount < getTextLength() - 1)
{
if(curChar[0] < ' ' && curChar[0] != '\n')
{
charCount++;
curLen = _unicodeFunctions.bringToNextUnichar(it,end);
curChar = getText().substr(bytesSkipped,curLen);
bytesSkipped += curLen;
}
else
{
break;
}
}
if(curChar[0] == '\n')
{
charCount++;
curLen = _unicodeFunctions.bringToNextUnichar(it,end);
curChar = getText().substr(bytesSkipped,curLen);
bytesSkipped += curLen;
}
}
for (int i = 0; i < column; ++i)
{
do
{
curLen = _unicodeFunctions.bringToNextUnichar(it,end);
curChar = getText().substr(bytesSkipped,curLen);
bytesSkipped += curLen;
charCount++;
}
while(curChar[0] < ' ');
}

return charCount - 1;
}

//and the opposite
AguiPoint AguiTextBox::columnRowFromIndex( int index )
{
std::string::const_iterator it = getText().begin();
std::string::const_iterator end = getText().end();

std::string curChar;
int charCount = 0;
int len = 0;
int byteCount = 0;
int curLen = 0;
for(int i = 0; i < (int)textRows.size(); ++i)
{
len = _unicodeFunctions.getUtf8StringLength(textRows[i]);

for(int j = 0; j < len; ++j)
{

//skip characters if needed
curLen = _unicodeFunctions.bringToNextUnichar(it,end);
curChar = getText().substr(byteCount,curLen);
byteCount += curLen;

while (curChar[0] < ' ' && curChar[0] != '\n')
{
curLen = _unicodeFunctions.bringToNextUnichar(it,end);
curChar = getText().substr(byteCount,curLen);
byteCount += curLen;
}
if(curChar[0] == '\n')
{
charCount++;
if(charCount == index)
{
return AguiPoint(j,i);
}

}
charCount++;
if(charCount == index)
{
return AguiPoint(j,i);
}



}
}
return AguiPoint(len,textRows.size() - 1);
}

最佳答案

我不清楚您需要对数据做什么。你是想写一个编辑器还是什么?

您可以做的一件事是构建一个指向每行开头的指针数组,也许还有每行的长度。

这样,您可以在不修改基础数据的情况下表示所有行。

但是您真的应该更好地准确描述转换数据的要求。

关于c++ - 在给定列和行的字符数组中查找索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4869488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com