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


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

任何帮助将是巨大的!

最佳答案

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

10-08 07:52
查看更多