FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
librime-lua-script/lua/plugins/regex.cc at main · shewer/librime-lua-script · GitHub
shewer
librime-lua-script
Repository navigation
Code
Issues
4
(4)
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
librime-lua-script
/
lua
/
plugins
/
regex.cc
Copy path
More file actions
More file actions
Latest commit
History
History
History
65 lines (56 loc) · 1.47 KB
Breadcrumbs
librime-lua-script
/
lua
/
plugins
/
regex.cc
Copy path
File metadata and controls
65 lines (56 loc) · 1.47 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
<
boost/regex.hpp
>
#
include
<
iostream
>
using
namespace
std
;
#
ifdef
__cplusplus
extern
"
C
"
{
#endif
#
include
<
lua.h
>
#
include
<
lualib.h
>
#
include
<
lauxlib.h
>
typedef
void
Register
(lua_State*);
static
int
regex_match
(lua_State* L){
if
(
2
>
lua_gettop
(L))
return
0
;
const
string
target
(
lua_tostring
(L,
1
) );
boost::regex
reg
(
lua_tostring
(L,
2
) ) ;
bool
res =
boost::regex_match
( target, reg );
lua_pushboolean
(L, res);
return
1
;
}
static
int
regex_replace
(lua_State* L) {
if
(
3
>
lua_gettop
(L))
return
0
;
const
string
target
(
lua_tostring
(L,
1
) );
boost::regex
reg
(
lua_tostring
(L,
2
) );
const
string
fmt
(
lua_tostring
(L,
3
) );
string res =
boost::regex_replace
(target, reg, fmt);
lua_pushstring
(L, res.
c_str
());
return
1
;
}
static
int
regex_search
(lua_State *L) {
if
(
2
>
lua_gettop
(L))
return
0
;
const
string
target
(
lua_tostring
(L,
1
));
boost::regex
reg
(
lua_tostring
(L,
2
) );
boost::smatch what;
if
(
boost::regex_search
(target,what, reg)) {
lua_newtable
(L);
for
(
int
i=
0
; i< what.
size
(); i++) {
lua_pushstring
(L, what[i].
str
().
c_str
() );
lua_rawseti
(L, -
2
,i+
1
);
}
return
1
;
}
return
0
;
}
static
const
struct
luaL_Reg
funcs[] =
{
{
"
match
"
, regex_match},
{
"
replace
"
, regex_replace},
{
"
search
"
, regex_search},
{
NULL
,
NULL
},
};
int
luaopen_regex
(lua_State* L){
luaL_newlib
(L, funcs );
return
1
;
}
#
ifdef
__cplusplus
}
#
endif
Back
|
FazBrowse Home
|
New Git URL