Exemple #1
0
void EscapeHtmlChars(Wtroka& str) {
    static const Wtroka lt(LT, ARRAY_SIZE(LT));
    static const Wtroka gt(GT, ARRAY_SIZE(GT));
    static const Wtroka amp(AMP, ARRAY_SIZE(AMP));
    static const Wtroka br(BR, ARRAY_SIZE(BR));
    static const Wtroka quot(QUOT, ARRAY_SIZE(QUOT));

    size_t escapedLen = 0;

    const Wtroka& cs = str;

    for (size_t i = 0; i < cs.size(); ++i)
        escapedLen += EscapedLen<insertBr>(cs[i]);

    if (escapedLen == cs.size())
        return;

    Wtroka res;
    res.reserve(escapedLen);

    size_t start = 0;

    for (size_t i = 0; i < cs.size(); ++i) {
        const Wtroka* ent = NULL;
        switch (cs[i]) {
            case '<':
                ent = &lt;
                break;
            case '>':
                ent = &gt;
                break;
            case '&':
                ent = &amp;
                break;
            case '\"':
                ent = &quot;
                break;
            default:
                if (insertBr && (cs[i] == '\r' || cs[i] == '\n')) {
                    ent = &br;
                    break;
                } else
                    continue;
        }

        res.append(cs.begin() + start, cs.begin() + i);
        res.append(ent->begin(), ent->end());
        start = i + 1;
    }

    res.append(cs.begin() + start, cs.end());
    res.swap(str);
}
Wtroka CWordBase::GetOriginalText() const
{
    //затычка для дурацких символов, порождаемых для <BR> после конца предложения
    if (m_typ == Punct && IsChar(m_txt, 'W'))
        return Wtroka();

    size_t quoteCount = (m_bHasOpenQuote ? 1 : 0) + (m_bHasCloseQuote ? 1 : 0);

    // optimize most frequent case
    if (quoteCount == 0)
        return m_txt;

    Wtroka res;
    res.reserve(m_txt.size() + quoteCount);
    if (m_bHasOpenQuote)
        res += m_bSingleOpenQuote ? SINGLE_QUOTE_CHAR : DOUBLE_QUOTE_CHAR;
    res += m_txt;
    if (m_bHasCloseQuote)
        res += m_bSingleCloseQuote ? SINGLE_QUOTE_CHAR : DOUBLE_QUOTE_CHAR;
    return res;
}