| 1 |
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* libdatrie - Double-Array Trie Library |
| 4 |
* Copyright (C) 2006 Theppitak Karoonboonyanan <[email protected]> |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
*/ |
| 20 |
|
| 21 |
/* |
| 22 |
* trie-string.c - Dynamic string type for Trie alphabets |
| 23 |
* Created: 2012-08-02 |
| 24 |
* Author: Theppitak Karoonboonyanan <[email protected]> |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "trie-string.h" |
| 28 |
#include "dstring-private.h" |
| 29 |
#include "triedefs.h" |
| 30 |
#include "trie-private.h" |
| 31 |
|
| 32 |
#include <string.h> |
| 33 |
#include <stdlib.h> |
| 34 |
|
| 35 |
/*----------------------------* |
| 36 |
* STATIC TRIECHAR STRING * |
| 37 |
*----------------------------*/ |
| 38 |
|
| 39 |
size_t |
| 40 |
trie_char_strlen (const TrieChar *str) |
| 41 |
{ |
| 42 |
size_t len = 0; |
| 43 |
while (*str++ != TRIE_CHAR_TERM) { |
| 44 |
++len; |
| 45 |
} |
| 46 |
return len; |
| 47 |
} |
| 48 |
|
| 49 |
size_t |
| 50 |
trie_char_strsize (const TrieChar *str) |
| 51 |
{ |
| 52 |
return trie_char_strlen (str) * sizeof (TrieChar); |
| 53 |
} |
| 54 |
|
| 55 |
TrieChar * |
| 56 |
trie_char_strdup (const TrieChar *str) |
| 57 |
{ |
| 58 |
TrieChar *dup |
| 59 |
= (TrieChar *) malloc (sizeof (TrieChar) * (trie_char_strlen (str) + 1)); |
| 60 |
if (UNLIKELY (!dup)) |
| 61 |
return NULL; |
| 62 |
TrieChar *p = dup; |
| 63 |
|
| 64 |
while (*str != TRIE_CHAR_TERM) { |
| 65 |
*p++ = *str++; |
| 66 |
} |
| 67 |
*p = TRIE_CHAR_TERM; |
| 68 |
|
| 69 |
return dup; |
| 70 |
} |
| 71 |
|
| 72 |
/*-----------------------------* |
| 73 |
* DYNAMIC TRIECHAR STRING * |
| 74 |
*-----------------------------*/ |
| 75 |
|
| 76 |
struct _TrieString { |
| 77 |
DString ds; |
| 78 |
}; |
| 79 |
|
| 80 |
|
| 81 |
TrieString * |
| 82 |
trie_string_new (int n_elm) |
| 83 |
{ |
| 84 |
return (TrieString *) dstring_new (sizeof (TrieChar), n_elm); |
| 85 |
} |
| 86 |
|
| 87 |
void |
| 88 |
trie_string_free (TrieString *ts) |
| 89 |
{ |
| 90 |
dstring_free ((DString *)ts); |
| 91 |
} |
| 92 |
|
| 93 |
int |
| 94 |
trie_string_length (const TrieString *ts) |
| 95 |
{ |
| 96 |
return dstring_length ((DString *)ts); |
| 97 |
} |
| 98 |
|
| 99 |
const void * |
| 100 |
trie_string_get_val (const TrieString *ts) |
| 101 |
{ |
| 102 |
return dstring_get_val ((DString *)ts); |
| 103 |
} |
| 104 |
|
| 105 |
void * |
| 106 |
trie_string_get_val_rw (TrieString *ts) |
| 107 |
{ |
| 108 |
return dstring_get_val_rw ((DString *)ts); |
| 109 |
} |
| 110 |
|
| 111 |
void |
| 112 |
trie_string_clear (TrieString *ts) |
| 113 |
{ |
| 114 |
dstring_clear ((DString *)ts); |
| 115 |
} |
| 116 |
|
| 117 |
Bool |
| 118 |
trie_string_copy (TrieString *dst, const TrieString *src) |
| 119 |
{ |
| 120 |
return dstring_copy ((DString *)dst, (const DString *)src); |
| 121 |
} |
| 122 |
|
| 123 |
Bool |
| 124 |
trie_string_append (TrieString *dst, const TrieString *src) |
| 125 |
{ |
| 126 |
return dstring_append ((DString *)dst, (const DString *)src); |
| 127 |
} |
| 128 |
|
| 129 |
Bool |
| 130 |
trie_string_append_string (TrieString *ts, const TrieChar *str) |
| 131 |
{ |
| 132 |
return dstring_append_string ((DString *)ts, |
| 133 |
str, strlen ((const char *)str)); |
| 134 |
} |
| 135 |
|
| 136 |
Bool |
| 137 |
trie_string_append_char (TrieString *ts, TrieChar tc) |
| 138 |
{ |
| 139 |
return dstring_append_char ((DString *)ts, &tc); |
| 140 |
} |
| 141 |
|
| 142 |
Bool |
| 143 |
trie_string_terminate (TrieString *ts) |
| 144 |
{ |
| 145 |
static const TrieChar term = TRIE_CHAR_TERM; |
| 146 |
return dstring_append_char ((DString *)ts, &term); |
| 147 |
} |
| 148 |
|
| 149 |
Bool |
| 150 |
trie_string_cut_last (TrieString *ts) |
| 151 |
{ |
| 152 |
return dstring_cut_last ((DString *)ts); |
| 153 |
} |
| 154 |
|
| 155 |
/* |
| 156 |
vi:ts=4:ai:expandtab |
| 157 |
*/ |