summaryrefslogtreecommitdiffstats
path: root/src/storage/punct_table.cpp
blob: d901dedb7f4b72c7af233bb9a3810d72ae7cac9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* 
 *  libpinyin
 *  Library to deal with pinyin.
 *  
 *  Copyright (C) 2024 Peng Wu <alexepico@gmail.com>
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "punct_table.h"

using namespace pinyin;

PunctTableEntry::PunctTableEntry() {
    m_ucs4_cache = g_array_new(TRUE, TRUE, sizeof(ucs4_t));
    m_utf8_cache = g_string_new(NULL);
}

PunctTableEntry::~PunctTableEntry() {
    g_array_free(m_ucs4_cache, TRUE);
    m_ucs4_cache = NULL;
    g_string_free(m_utf8_cache, TRUE);
    m_utf8_cache = NULL;
}

bool PunctTableEntry::escape(const gchar * punct, gint maxlen = -1) {
    if (maxlen == -1)
        maxlen = G_MAXINT;

    g_array_set_size(m_ucs4_cache, 0);

    glong ucs4_len = 0;
    gunichar * ucs4_str = g_utf8_to_ucs4(punct, maxlen, NULL, &ucs4_len, NULL);

    for(int i = 0; i < ucs4_len; ++i) {
        g_array_append_val(m_ucs4_cache, ucs4_str[i]);
        if (i < ucs4_len - 1)
            g_array_append_val(m_ucs4_cache, ucs4_str[i]);
    }

    g_free(ucs4_str);
    return true;
}

int PunctTableEntry::unescape(const ucs4_t * punct, gint maxlen = -1) {
    if (maxlen == -1)
        maxlen = G_MAXINT;

    g_string_set_size(m_utf8_cache, 0);

    int index = 0;
    while (index < maxlen) {
        g_string_append_unichar(m_utf8_cache, punct[index]);
        index++;
        if (index >= maxlen)
            break;
        if (punct[index - 1] == punct[index])
            index++;
        else
            break;
    }

    return index;
}

bool PunctTableEntry::get_all_punctuations(gchar ** & puncts) {
    assert(puncts == NULL);

    size_t size = m_chunk.size();
    if (size == 0)
        return false;

    GPtrArray * array = g_ptr_array_new();
    ucs4_t * begin = (ucs4_t *) m_chunk.begin();
    ucs4_t * end = (ucs4_t *) m_chunk.end();

    while (begin < end) {
        int len = unescape(begin, end - begin);
        g_ptr_array_add(array, g_strdup(m_utf8_cache->str));
        begin += len;
    }

    g_ptr_array_add(array, NULL);
    /* must be freed by g_strfreev. */
    puncts = (gchar **) g_ptr_array_free(array, FALSE);
    return true;
}

bool PunctTableEntry::append_punctuation(const gchar * punct) {
    gchar ** puncts = NULL;

    get_all_punctuations(puncts);
    if (puncts && g_strv_contains(puncts, punct))
        abort();
    g_strfreev(puncts);

    if (!escape(punct))
        return false;

    m_chunk.append_content
        (m_ucs4_cache->data, m_ucs4_cache->len * sizeof(ucs4_t));

    return true;
}

bool PunctTableEntry::remove_punctuation(const gchar * punct) {
    if (m_chunk.size() == 0)
        return false;

    if (!escape(punct))
        return false;

    ucs4_t * begin = (ucs4_t *) m_chunk.begin();
    ucs4_t * end = (ucs4_t *) m_chunk.end();
    int index = 0;

    int len = m_ucs4_cache->len;
    while (begin + index + len <= end) {
        /* match the punctuation */
        if (0 == memcmp(begin + index, m_ucs4_cache->data,
                        len * sizeof(ucs4_t))) {
            m_chunk.remove_content
                (index * sizeof(ucs4_t),
                 len * sizeof(ucs4_t));
            return true;
        }

        /* check the next punctuation index */
        index++;
        while (begin + index < end) {
            if (begin[index - 1] == begin[index])
                index += 2;
            else
                break;
        }
    }

    return false;
}