Merge lp:~paul-lucas/zorba/pjl-misc into lp:zorba

Proposed by Paul J. Lucas
Status: Merged
Approved by: Paul J. Lucas
Approved revision: 11353
Merged at revision: 11653
Proposed branch: lp:~paul-lucas/zorba/pjl-misc
Merge into: lp:zorba
Diff against target: 348 lines (+70/-38)
8 files modified
src/runtime/full_text/apply.cpp (+2/-2)
src/runtime/full_text/ft_module_impl.cpp (+8/-16)
src/runtime/full_text/icu_tokenizer.cpp (+3/-3)
src/runtime/full_text/stemmer/sb_stemmer.cpp (+1/-1)
src/runtime/full_text/thesauri/wn_thesaurus.cpp (+1/-1)
src/util/locale.cpp (+29/-8)
src/util/locale.h (+25/-6)
src/zorbatypes/ft_token.cpp (+1/-1)
To merge this branch: bzr merge lp:~paul-lucas/zorba/pjl-misc
Reviewer Review Type Date Requested Status
Chris Hillery Approve
Paul J. Lucas Approve
Review via email: mp+189748@code.launchpad.net

Commit message

Added a str() function for various locale enum string look-ups to ensure the index is never out of bounds.

Description of the change

Added a str() function for various locale enum string look-ups to ensure the index is never out of bounds.

To post a comment you must log in.
lp:~paul-lucas/zorba/pjl-misc updated
11353. By Paul J. Lucas

Added str() functions.

Revision history for this message
Paul J. Lucas (paul-lucas) :
review: Approve
Revision history for this message
Chris Hillery (ceejatec) :
review: Approve
Revision history for this message
Zorba Build Bot (zorba-buildbot) wrote :

Validation queue starting for the following merge proposals:
https://code.launchpad.net/~paul-lucas/zorba/pjl-misc/+merge/189748

Progress dashboard at http://jenkins.lambda.nu/view/ValidationQueue

Revision history for this message
Zorba Build Bot (zorba-buildbot) wrote :

Validation queue succeeded - proposal merged!

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/runtime/full_text/apply.cpp'
2--- src/runtime/full_text/apply.cpp 2013-09-17 21:12:49 +0000
3+++ src/runtime/full_text/apply.cpp 2013-10-08 00:56:29 +0000
4@@ -1239,7 +1239,7 @@
5 throw XQUERY_EXCEPTION(
6 err::FTST0009,
7 ERROR_PARAMS(
8- iso639_1::string_of[ qt0.lang() ], ZED( FTST0009_BadThesaurusLang )
9+ iso639_1::str( qt0.lang() ), ZED( FTST0009_BadThesaurusLang )
10 )
11 );
12
13@@ -1262,7 +1262,7 @@
14 throw XQUERY_EXCEPTION(
15 err::FTST0009,
16 ERROR_PARAMS(
17- iso639_1::string_of[ qt0.lang() ], ZED( FTST0009_BadTokenizerLang )
18+ iso639_1::str( qt0.lang() ), ZED( FTST0009_BadTokenizerLang )
19 )
20 );
21
22
23=== modified file 'src/runtime/full_text/ft_module_impl.cpp'
24--- src/runtime/full_text/ft_module_impl.cpp 2013-09-17 21:12:49 +0000
25+++ src/runtime/full_text/ft_module_impl.cpp 2013-10-08 00:56:29 +0000
26@@ -107,9 +107,7 @@
27 if ( !provider->getTokenizer( lang, t_state, &tokenizer ) )
28 throw XQUERY_EXCEPTION(
29 err::FTST0009 /* lang not supported */,
30- ERROR_PARAMS(
31- iso639_1::string_of[ lang ], ZED( FTST0009_BadTokenizerLang )
32- ),
33+ ERROR_PARAMS( iso639_1::str( lang ), ZED( FTST0009_BadTokenizerLang ) ),
34 ERROR_LOC( loc )
35 );
36 return std::move( tokenizer );
37@@ -124,7 +122,7 @@
38 s = "lang";
39 GENV_ITEMFACTORY->createString( item, s );
40 keys.push_back( item );
41- s = iso639_1::string_of[ token.lang() ];
42+ s = iso639_1::str( token.lang() );
43 GENV_ITEMFACTORY->createString( item, s );
44 values.push_back( item );
45 }
46@@ -224,7 +222,7 @@
47 static_context const *const sctx = getStaticContext();
48 ZORBA_ASSERT( sctx );
49 iso639_1::type const lang = get_lang_from( sctx );
50- zstring lang_string( iso639_1::string_of[ lang ] );
51+ zstring lang_string( iso639_1::str( lang ) );
52
53 PlanIteratorState *state;
54 DEFAULT_STACK_INIT( PlanIteratorState, state, plan_state );
55@@ -240,7 +238,7 @@
56 bool HostLangIterator::nextImpl( store::Item_t &result,
57 PlanState &plan_state ) const {
58 iso639_1::type const lang = GENV.get_host_lang();
59- zstring lang_string = iso639_1::string_of[ lang ];
60+ zstring lang_string = iso639_1::str( lang );
61
62 PlanIteratorState *state;
63 DEFAULT_STACK_INIT( PlanIteratorState, state, plan_state );
64@@ -307,9 +305,7 @@
65 if ( !stop_words )
66 throw XQUERY_EXCEPTION(
67 err::FTST0009 /* lang not supported */,
68- ERROR_PARAMS(
69- iso639_1::string_of[ lang ], ZED( FTST0009_BadStopWordsLang )
70- ),
71+ ERROR_PARAMS( iso639_1::str( lang ), ZED( FTST0009_BadStopWordsLang ) ),
72 ERROR_LOC( loc )
73 );
74 GENV_ITEMFACTORY->createBoolean( result, stop_words->contains( word ) );
75@@ -459,9 +455,7 @@
76 } else {
77 throw XQUERY_EXCEPTION(
78 err::FTST0009 /* lang not supported */,
79- ERROR_PARAMS(
80- iso639_1::string_of[ lang ], ZED( FTST0009_BadStemmerLang )
81- ),
82+ ERROR_PARAMS( iso639_1::str( lang ), ZED( FTST0009_BadStemmerLang ) ),
83 ERROR_LOC( loc )
84 );
85 }
86@@ -557,9 +551,7 @@
87 if ( !provider->getThesaurus( lang, &state->thesaurus_ ) )
88 throw XQUERY_EXCEPTION(
89 err::FTST0009 /* lang not supported */,
90- ERROR_PARAMS(
91- iso639_1::string_of[ lang ], ZED( FTST0009_BadThesaurusLang )
92- ),
93+ ERROR_PARAMS( iso639_1::str( lang ), ZED( FTST0009_BadThesaurusLang ) ),
94 ERROR_LOC( loc )
95 );
96
97@@ -842,7 +834,7 @@
98 { // local scope
99 vector<store::Item_t> langs;
100 FOR_EACH( Tokenizer::Properties::languages_type, i, props.languages ) {
101- s = iso639_1::string_of[ *i ];
102+ s = iso639_1::str( *i );
103 GENV_ITEMFACTORY->createString( item, s );
104 langs.push_back( item );
105 }
106
107=== modified file 'src/runtime/full_text/icu_tokenizer.cpp'
108--- src/runtime/full_text/icu_tokenizer.cpp 2013-08-02 18:32:47 +0000
109+++ src/runtime/full_text/icu_tokenizer.cpp 2013-10-08 00:56:29 +0000
110@@ -128,10 +128,10 @@
111 return i->second;
112
113 iso3166_1::type const country_code = GENV.get_host_country();
114- char const *const country = country_code != iso3166_1::unknown ?
115- iso3166_1::string_of[ country_code ] : nullptr;
116+ char const *const country = country_code ?
117+ iso3166_1::str( country_code ) : nullptr;
118 Locale &icu_locale = locale_cache[ lang ];
119- icu_locale = Locale( iso639_1::string_of[ lang ], country );
120+ icu_locale = Locale( iso639_1::str( lang ), country );
121 return icu_locale;
122 }
123
124
125=== modified file 'src/runtime/full_text/stemmer/sb_stemmer.cpp'
126--- src/runtime/full_text/stemmer/sb_stemmer.cpp 2013-06-14 22:58:42 +0000
127+++ src/runtime/full_text/stemmer/sb_stemmer.cpp 2013-10-08 00:56:29 +0000
128@@ -57,7 +57,7 @@
129 ///////////////////////////////////////////////////////////////////////////////
130
131 SnowballStemmer::SnowballStemmer( iso639_1::type lang ) :
132- stemmer_( sb_stemmer_new( iso639_1::string_of[ lang ], nullptr ) )
133+ stemmer_( sb_stemmer_new( iso639_1::str( lang ), nullptr ) )
134 {
135 ZORBA_FATAL( stemmer_, "out of memory" );
136 }
137
138=== modified file 'src/runtime/full_text/thesauri/wn_thesaurus.cpp'
139--- src/runtime/full_text/thesauri/wn_thesaurus.cpp 2013-06-12 00:21:05 +0000
140+++ src/runtime/full_text/thesauri/wn_thesaurus.cpp 2013-10-08 00:56:29 +0000
141@@ -65,7 +65,7 @@
142 */
143 static void append_wordnet_file( zstring &path, iso639_1::type lang ) {
144 fs::append( path, "wordnet-" );
145- path += iso639_1::string_of[ lang ];
146+ path += iso639_1::str( lang );
147 path += ".zth";
148 }
149
150
151=== modified file 'src/util/locale.cpp'
152--- src/util/locale.cpp 2013-10-06 23:48:06 +0000
153+++ src/util/locale.cpp 2013-10-08 00:56:29 +0000
154@@ -44,6 +44,9 @@
155 static char const *const *const end = \
156 CHAR_ARRAY + sizeof( CHAR_ARRAY ) / sizeof( char* )
157
158+#define DEF_LENGTH(CHAR_ARRAY) \
159+ static size_t const length = sizeof( CHAR_ARRAY ) / sizeof( char* )
160+
161 #define FIND(WHAT) \
162 static_cast<type>( find_index( string_of, end, WHAT ) )
163
164@@ -84,10 +87,10 @@
165 iso3166_1::type country ) {
166 assert( lang );
167
168- zstring locale_name( iso639_1::string_of[ lang ] );
169+ zstring locale_name( iso639_1::str( lang ) );
170 if ( country ) {
171 locale_name += '-';
172- locale_name += iso3166_1::string_of[ country ];
173+ locale_name += iso3166_1::str( country );
174 }
175
176 unique_ptr<WCHAR[]> wlocale_name( new WCHAR[ LOCALE_NAME_MAX_LENGTH ] );
177@@ -240,15 +243,15 @@
178 */
179 static locale_t get_unix_locale_t( iso639_1::type lang,
180 iso3166_1::type country ) {
181- zstring locale_name( iso639_1::string_of[ lang ] );
182+ zstring locale_name( iso639_1::str( lang ) );
183 if ( country ) {
184 locale_name += '_';
185- locale_name += iso3166_1::string_of[ country ];
186+ locale_name += iso3166_1::str( country );
187 }
188 locale_name += ".UTF-8";
189 locale_t loc = ::newlocale( LC_TIME_MASK, locale_name.c_str(), nullptr );
190 if ( !loc && country ) { // try it without the country
191- locale_name = iso639_1::string_of[ lang ];
192+ locale_name = iso639_1::str( lang );
193 locale_name += ".UTF-8";
194 loc = ::newlocale( LC_TIME_MASK, locale_name.c_str(), nullptr );
195 }
196@@ -285,7 +288,7 @@
197
198 namespace iso3166_1 {
199
200-char const *const string_of[] = {
201+static char const *const string_of[] = {
202 "#UNKNOWN", // starts with '#' for sorting
203 "AD", // Andorra
204 "AE", // United Arab Emirates
205@@ -739,13 +742,19 @@
206 return lang_to_country[ lang ];
207 }
208
209+char const* str( type t ) {
210+ DEF_LENGTH( string_of );
211+ int const i = static_cast<int>( t );
212+ return i >= 0 && i < length ? string_of[ i ] : "UNKNOWN";
213+}
214+
215 } // namespace iso3166_1
216
217 ///////////////////////////////////////////////////////////////////////////////
218
219 namespace iso639_1 {
220
221-char const *const string_of[] = {
222+static char const *const string_of[] = {
223 "#UNKNOWN", // starts with '#' for sorting
224 "aa", // Afar
225 "ab", // Abkhazian
226@@ -940,13 +949,19 @@
227 return FIND( lang );
228 }
229
230+char const* str( type t ) {
231+ DEF_LENGTH( string_of );
232+ int const i = static_cast<int>( t );
233+ return i >= 0 && i < length ? string_of[ i ] : "UNKNOWN";
234+}
235+
236 } // namespace iso639_1
237
238 ///////////////////////////////////////////////////////////////////////////////
239
240 namespace iso639_2 {
241
242-char const *const string_of[] = {
243+static char const *const string_of[] = {
244 "#UNKNOWN", // starts with '#' for sorting
245 "aar", // Afar
246 "abk", // Abkhazian
247@@ -1078,6 +1093,12 @@
248 return FIND( lang );
249 }
250
251+char const* str( type t ) {
252+ DEF_LENGTH( string_of );
253+ int const i = static_cast<int>( t );
254+ return i >= 0 && i < length ? string_of[ i ] : "UNKNOWN";
255+}
256+
257 } // namespace iso639_2
258
259 ///////////////////////////////////////////////////////////////////////////////
260
261=== modified file 'src/util/locale.h'
262--- src/util/locale.h 2013-06-11 23:38:49 +0000
263+++ src/util/locale.h 2013-10-08 00:56:29 +0000
264@@ -30,7 +30,13 @@
265 ///////////////////////////////////////////////////////////////////////////
266
267 namespace iso3166_1 {
268- extern char const *const string_of[];
269+ /**
270+ * Maps an iso3166_1::type to a string.
271+ *
272+ * @param t The type to map.
273+ * @return Returns the corresponding string or "UNKNOWN".
274+ */
275+ char const* str( type t );
276
277 /**
278 * Emits an iso3166_1::type to an ostream.
279@@ -40,7 +46,7 @@
280 * @return Returns \a o.
281 */
282 inline std::ostream& operator<<( std::ostream &o, type t ) {
283- return o << string_of[ t ];
284+ return o << str( t );
285 }
286
287 /**
288@@ -73,7 +79,13 @@
289 ///////////////////////////////////////////////////////////////////////////
290
291 namespace iso639_1 {
292- extern char const *const string_of[];
293+ /**
294+ * Maps an iso639_1::type to a string.
295+ *
296+ * @param t The type to map.
297+ * @return Returns the corresponding string or "UNKNOWN".
298+ */
299+ char const* str( type t );
300
301 /**
302 * Emits an iso639_1::type to an ostream.
303@@ -83,7 +95,7 @@
304 * @return Returns \a o.
305 */
306 inline std::ostream& operator<<( std::ostream &o, type t ) {
307- return o << string_of[ t ];
308+ return o << str( t );
309 }
310
311 /**
312@@ -234,7 +246,14 @@
313 zul, ///< Zulu
314 NUM_ENTRIES
315 };
316- extern char const *const string_of[];
317+
318+ /**
319+ * Maps an iso639_2::type to a string.
320+ *
321+ * @param t The type to map.
322+ * @return Returns the corresponding string or "UNKNOWN".
323+ */
324+ char const* str( type t );
325
326 /**
327 * Emits an iso639_2::type to an ostream.
328@@ -244,7 +263,7 @@
329 * @return Returns \a o.
330 */
331 inline std::ostream& operator<<( std::ostream &o, type t ) {
332- return o << string_of[ t ];
333+ return o << str( t );
334 }
335
336 /**
337
338=== modified file 'src/zorbatypes/ft_token.cpp'
339--- src/zorbatypes/ft_token.cpp 2013-06-12 00:21:05 +0000
340+++ src/zorbatypes/ft_token.cpp 2013-10-08 00:56:29 +0000
341@@ -177,7 +177,7 @@
342
343 std::ostream& operator<<( ostream &o, FTToken const &t ) {
344 return o << "[\"" << t.value() << "\" ("
345- << iso639_1::string_of[ t.lang() ] << ") "
346+ << iso639_1::str( t.lang() ) << ") "
347 << t.pos() << ',' << t.sent() << ',' << t.para() << ']';
348 }
349

Subscribers

People subscribed via source and target branches