gpt4 book ai didi

c++ - 在插入符号下获取单词 - C++、wxWidgets

转载 作者:行者123 更新时间:2023-11-28 08:21:52 25 4
gpt4 key购买 nike

我正在使用 wxWidgets 框架编写一个文本编辑器。我需要从文本控件中获取插入符号下的单词。这是我想出的。

static bool IsWordBoundary(wxString& text)
{
return (text.Cmp(wxT(" ")) == 0 ||
text.Cmp(wxT('\n')) == 0 ||
text.Cmp(wxT('\t')) == 0 ||
text.Cmp(wxT('\r')) == 0);
}

static wxString GetWordUnderCaret(wxTextCtrl* control)
{
int insertion_point = control->GetInsertionPoint();
wxTextPos last_position = control->GetLastPosition();
int start_at, ends_at = 0;

// Finding starting position:
// from the current caret position, move back each character until
// we hit a word boundary.
int caret_pos = insertion_point;
start_at = caret_pos;
while (caret_pos)
{
wxString text = control->GetRange (caret_pos - 1, caret_pos);
if (IsWordBoundary (text)) {
break;
}

start_at = --caret_pos;
}

// Finding ending position:
// from the current caret position, move forward each character until
// we hit a word boundary.
caret_pos = ends_at = insertion_point;
while (caret_pos < last_position)
{
wxString text = control->GetRange (caret_pos, caret_pos + 1);
if (IsWordBoundary (text)) {
break;
}

ends_at = ++caret_pos;
}

return (control->GetRange (start_at, ends_at));
}

此代码按预期工作。但我想知道这是解决问题的最佳方法吗?您是否看到对上述代码有任何可能的修复?

任何帮助都会很棒!

最佳答案

标点符号是单词的一部分吗?它在您的代码中 - 这是您想要的吗?

关于c++ - 在插入符号下获取单词 - C++、wxWidgets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5535438/

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