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

发布于 2024-10-29 08:38:41 字数 1476 浏览 6 评论 0原文

我正在使用 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));
}

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

任何帮助都会很棒!

I am writing a text editor using the wxWidgets framework. I need to get the word under caret from the text control. Here is what I came up with.

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));
}

This code works as expected. But I am wondering is this the best way to approach the problem? Do you see any possible fixes on the above code?

Any help would be great!

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

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

发布评论

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

评论(2

盛夏已如深秋| 2024-11-05 08:38:41

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

Is punctuation part of a word? It is in your code -- is that what you want?

樱桃奶球 2024-11-05 08:38:41

我的做法如下:

wxString word_boundary_marks = " \n\t\r";
wxString text_in_control     = control->GetValue();
int ends_at                  = text_in_control.find_first_of( word_boundary_marks, insertion_point) - 1;
int start_at                 = text_in_control.Mid(0,insertion_point).find_last_of(word_boundary_marks) + 1;

我还没有对此进行测试,因此可能存在一两个“差一”错误,您应该添加对“未找到”、字符串结尾和任何其他单词标记的检查。我的代码应该为您提供所需的基础。

Here is how I would do it:

wxString word_boundary_marks = " \n\t\r";
wxString text_in_control     = control->GetValue();
int ends_at                  = text_in_control.find_first_of( word_boundary_marks, insertion_point) - 1;
int start_at                 = text_in_control.Mid(0,insertion_point).find_last_of(word_boundary_marks) + 1;

I haven't tested this, so there likely are one or two "off-by-one" errors and you should add checks for "not found", end of string, and any other word markers. My code should give you the basis for what you need.

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