summaryrefslogtreecommitdiff
path: root/extensions/fts++/stringutils.cpp
blob: 59a6dd2c0e615a35b554bb965d7178a83b26e76c (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Copyright (C) 2012 Mikkel Kamstrup Erlandsen
 *
 * 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 2
 * 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/>.
 *
 * Authored by Mikkel Kamstrup Erlandsen <mikkel.kamstrup@gmail.com>
 *
 */
#include <string>

#include "stringutils.h"

#ifdef HAVE_DEE_ICU
#include <dee-icu.h>
#endif

using namespace std;

namespace ZeitgeistFTS {

namespace StringUtils {

/**
 * Make sure s has equal or less than 'nbytes' bytes making sure the returned
 * string is still valid UTF-8.
 *
 * NOTE: It is assumed the input string is valid UTF-8. Untrusted text
 * should be validated with g_utf8_validate().
 *
 * This function useful for working with Xapian terms because Xapian has
 * a max term length of 245 (which is not very well documented, but see
 * http://xapian.org/docs/omega/termprefixes.html).
 */
string Truncate (string const& s, unsigned int nbytes)
{
  const gchar *str = s.c_str();
  const gchar *iter = str;

  nbytes = MIN(nbytes, s.length());

  while (iter - str < nbytes)
  {
    const gchar *tmp = g_utf8_next_char (iter);
    if (tmp - str > nbytes) break;
    iter = tmp;
  }


  return s.substr(0, iter - str);
}

/**
 * Converts a URI into an index- and query friendly string. The problem
 * is that Xapian doesn't handle CAPITAL letters or most non-alphanumeric
 * symbols in a boolean term when it does prefix matching. The mangled
 * URIs returned from this function are suitable for boolean prefix searches.
 *                 
 * IMPORTANT: This is a 1-way function! You can not convert back.
 */
string MangleUri (string const& orig)
{
  string s(orig);
  size_t pos = 0;
  while ((pos = s.find_first_of (": /", pos)) != string::npos)
  {
    s.replace (pos, 1, 1, '_');
    pos++;
  }

  return s;
}

/**
 * This method expects a valid uri and tries to split it into authority,
 * path and query.
 *
 * Note that any and all parts may be left untouched.
 */
void SplitUri (string const& uri, string &authority,
               string &path, string &query)
{
  size_t colon_pos = uri.find (':');
  if (colon_pos == string::npos) return; // not an uri?
  bool has_double_slash = uri.length () > colon_pos + 2 && 
    uri.compare (colon_pos + 1, 2, "//") == 0;

  size_t start_pos = has_double_slash ? colon_pos + 3 : colon_pos + 1;

  size_t first_slash = uri.find ('/', start_pos);
  size_t question_mark_pos = uri.find ('?', first_slash == string::npos ?
      start_pos : first_slash + 1);

  authority = uri.substr (start_pos);
  if (first_slash != string::npos)
  {
    authority.resize (first_slash - start_pos);
  }
  else if (question_mark_pos != string::npos)
  {
    authority.resize (question_mark_pos - start_pos);
  }

  if (first_slash == string::npos)
  {
    first_slash = start_pos + authority.length ();
  }

  if (question_mark_pos != string::npos)
  {
    path = uri.substr (first_slash, question_mark_pos - first_slash);
    query = uri.substr (question_mark_pos + 1);
  }
  else
  {
    path = uri.substr (first_slash);
  }
}

#ifdef HAVE_DEE_ICU
static DeeICUTermFilter *icu_filter = NULL;

/**
 * Use ascii folding filter on the input text and return folded version
 * of the original string.
 *
 * Note that if the folded version is exactly the same as the original
 * empty string will be returned.
 */
string AsciiFold (string const& input)
{
  if (icu_filter == NULL)
  {
    icu_filter = dee_icu_term_filter_new_ascii_folder ();
    if (icu_filter == NULL) return "";
  }

  // FIXME: check first if the input contains any non-ascii chars?

  gchar *folded = dee_icu_term_filter_apply (icu_filter, input.c_str ());
  string result (folded);
  g_free (folded);

  return result == input ? "" : result;
}
#else
string AsciiFold (string const& input)
{
  return "";
}
#endif

} /* namespace StringUtils */

} /* namespace ZeitgeistFTS */