c++wxwidgetsscintillawxstyledtextctrl

wxStyledTextCtrl non ASCII characters


I realized that in wxStyledTextCtrl if the user's comments contains non-ASCII characters, the positions reported by WordStartPosition and WordEndPosition are wrong. What is a good way of dealing with non-ASCII characters in wxStyledTextCtrl? How can I identify the characters that are non-ASCII?


Solution

  • You've probably answered this question by now, but in the experiments I've done, WordStartPosition and WordEndPosition still work with non-ASCII characters. The data internally in the control is stored in UTF-8 format, and those functions give the number of bytes in that data where the word starts and ends. If that's not what's happening for you, can you post a sample where they don't work?

    As for determining which characters are and aren't ASCII, something like the following seems to work (assuming a is the start and b is the end position):

    wxString s = m_stc->GetTextRange(a,b);
    
    for (wxString::const_iterator i = s.begin(); i != s.end(); ++i)
    {
        wxUniChar uni_ch = *i;
    
        if(uni_ch.IsAscii())
        {
            //something
        }
        else
        {
            //something else
        }
    }
    

    One thing I did notice is that if you use a value for a or b that falls in the middle of one of the non-ASCII characters, the resulting string will be empty. I hope this of some help if you haven't already found a solution.