Repository navigation
Expand file tree
/
Copy pathexpression_node.cpp
More file actions
156 lines (143 loc) · 4.5 KB
/
Copy pathexpression_node.cpp
File metadata and controls
156 lines (143 loc) · 4.5 KB
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
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2014 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
#include <mapnik/expression_node.hpp>
#include <mapnik/value_types.hpp>
#include <mapnik/util/noncopyable.hpp>
#if defined(BOOST_REGEX_HAS_ICU)
#include <boost/regex/icu.hpp>
#else
#include <boost/regex.hpp>
#endif
namespace mapnik
{
struct _regex_match_impl : util::noncopyable {
#if defined(BOOST_REGEX_HAS_ICU)
_regex_match_impl(value_unicode_string const& ustr) :
pattern_(boost::make_u32regex(ustr)) {}
boost::u32regex pattern_;
#else
_regex_match_impl(std::string const& ustr) :
pattern_(ustr) {}
boost::regex pattern_;
#endif
};
struct _regex_replace_impl : util::noncopyable {
#if defined(BOOST_REGEX_HAS_ICU)
_regex_replace_impl(value_unicode_string const& ustr, value_unicode_string const& f) :
pattern_(boost::make_u32regex(ustr)),
format_(f) {}
boost::u32regex pattern_;
value_unicode_string format_;
#else
_regex_replace_impl(std::string const& ustr,std::string const& f) :
pattern_(ustr),
format_(f) {}
boost::regex pattern_;
std::string format_;
#endif
};
regex_match_node::regex_match_node(transcoder const& tr,
expr_node const& a,
std::string const& ustr)
: expr(a),
impl_(new _regex_match_impl(
#if defined(BOOST_REGEX_HAS_ICU)
tr.transcode(ustr.c_str())
#else
ustr
#endif
)) {}
value regex_match_node::apply(value const& v) const
{
auto const& pattern = impl_.get()->pattern_;
#if defined(BOOST_REGEX_HAS_ICU)
return boost::u32regex_match(v.to_unicode(),pattern);
#else
return boost::regex_match(v.to_string(),pattern);
#endif
}
std::string regex_match_node::to_string() const
{
std::string str_;
str_ +=".match('";
auto const& pattern = impl_.get()->pattern_;
#if defined(BOOST_REGEX_HAS_ICU)
std::string utf8;
value_unicode_string ustr = value_unicode_string::fromUTF32( &pattern.str()[0], pattern.str().length());
to_utf8(ustr,utf8);
str_ += utf8;
#else
str_ += pattern.str();
#endif
str_ +="')";
return str_;
}
regex_replace_node::regex_replace_node(transcoder const& tr,
expr_node const& a,
std::string const& ustr,
std::string const& f)
: expr(a),
impl_(new _regex_replace_impl(
#if defined(BOOST_REGEX_HAS_ICU)
tr.transcode(ustr.c_str()),
tr.transcode(f.c_str())
#else
ustr,
f
#endif
)) {}
value regex_replace_node::apply(value const& v) const
{
auto const& pattern = impl_.get()->pattern_;
auto const& format = impl_.get()->format_;
#if defined(BOOST_REGEX_HAS_ICU)
return boost::u32regex_replace(v.to_unicode(),pattern,format);
#else
std::string repl = boost::regex_replace(v.to_string(),pattern,format);
transcoder tr_("utf8");
return tr_.transcode(repl.c_str());
#endif
}
std::string regex_replace_node::to_string() const
{
std::string str_;
str_ +=".replace(";
str_ += "'";
auto const& pattern = impl_.get()->pattern_;
auto const& format = impl_.get()->format_;
#if defined(BOOST_REGEX_HAS_ICU)
std::string utf8;
value_unicode_string ustr = value_unicode_string::fromUTF32( &pattern.str()[0], pattern.str().length());
to_utf8(ustr,utf8);
str_ += utf8;
str_ +="','";
to_utf8(format ,utf8);
str_ += utf8;
#else
str_ += pattern.str();
str_ +="','";
str_ += format;
#endif
str_ +="')";
return str_;
}
}