1 /*
2 * Copyright 2006-2008 The FLWOR Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "stdafx.h"
17
18 #include <cstring>
19 #include <unicode/stsearch.h>
20
21 #include "util/unicode_util.h"
22 #include "zorbautils/string_util.h"
23 #include "zorbatypes/zstring.h"
24 #include "diagnostics/xquery_diagnostics.h"
25
26 using namespace std;
27 #ifndef ZORBA_NO_ICU
28 U_NAMESPACE_USE
29 #endif /* ZORBA_NO_ICU */
30
31 namespace zorba {
32 namespace utf8 {
33
34 ///////////////////////////////////////////////////////////////////////////////
35
find(char const * s,size_t s_len,char const * ss,size_t ss_len,XQPCollator const * collator)36 size_t find( char const *s, size_t s_len, char const *ss, size_t ss_len,
37 XQPCollator const *collator ) {
38 #ifndef ZORBA_NO_ICU
39 if ( !collator || collator->doMemCmp()) {
40 #endif /* ZORBA_NO_ICU */
41 char const *const result = ::strstr( s, ss );
42 return result ? result - s : zstring::npos;
43 #ifndef ZORBA_NO_ICU
44 }
45
46 unicode::string u_s, u_ss;
47 unicode::to_string( s, (unicode::size_type)s_len, &u_s );
48 unicode::to_string( ss, (unicode::size_type)ss_len, &u_ss );
49
50 UErrorCode err = U_ZERO_ERROR;
51 StringSearch search(
52 u_ss, u_s, static_cast<RuleBasedCollator*>( collator->getCollator() ), NULL,
53 err
54 );
55 if ( U_SUCCESS( err ) ) {
56 for ( long pos = search.first( err );
57 U_SUCCESS( err ) && pos != USEARCH_DONE;
58 pos = search.next( err ) ) {
59 assert(pos >= 0);
60 return static_cast<ulong>(pos);
61 }
62 }
63 return zstring::npos;
64 #endif /* ZORBA_NO_ICU */
65 }
66
67
rfind(char const * s,size_t s_len,char const * ss,size_t ss_len,XQPCollator const * collator)68 size_t rfind( char const *s, size_t s_len, char const *ss, size_t ss_len,
69 XQPCollator const *collator ) {
70 #ifndef ZORBA_NO_ICU
71 if ( ! collator || collator->doMemCmp()) {
72 #endif /* ZORBA_NO_ICU */
73 zstring_b tmp;
74 tmp.wrap_memory(const_cast<char*>(s), s_len);
75 return tmp.rfind(ss, ss_len);
76 #ifndef ZORBA_NO_ICU
77 }
78
79 unicode::string u_s, u_ss;
80 unicode::to_string( s, (unicode::size_type)s_len, &u_s );
81 unicode::to_string( ss, (unicode::size_type)ss_len, &u_ss );
82
83 UErrorCode err = U_ZERO_ERROR;
84
85 StringSearch search(u_ss,
86 u_s,
87 static_cast<RuleBasedCollator*>(collator->getCollator()),
88 NULL,
89 err);
90
91 if ( U_SUCCESS( err ) )
92 {
93 long pos = search.last(err);
94
95 if(U_SUCCESS(err) && pos != USEARCH_DONE)
96 {
97 assert(pos >= 0);
98 return static_cast<ulong>(pos);
99 }
100 }
101
102 return zstring::npos;
103 #endif /* ZORBA_NO_ICU */
104 }
105
match_part(char const * in,char const * pattern,char const * flags)106 bool match_part( char const *in, char const *pattern, char const *flags ) {
107 unicode::regex re;
108 re.compile( pattern, flags );
109 return re.match_part( in );
110 }
111
match_whole(char const * in,char const * pattern,char const * flags)112 bool match_whole( char const *in, char const *pattern, char const *flags ) {
113 unicode::regex re;
114 re.compile( pattern, flags );
115 return re.match_whole( in );
116 }
117
118 ///////////////////////////////////////////////////////////////////////////////
119
120 } // namespace utf8
121 } // namespace zorba
122 /* vim:set et sw=2 ts=2: */
123