// 增加了 str_find str_gsub 两个函数
// 测试如下
// str_gsub( "yangzm 32zm", "zm", "12" );
// str_find( "yangzm", "zm" );
static const char *lmemfind (const char *s1, size_t l1,
const char *s2, size_t l2)
{
if (l2 == 0) return s1; /* empty strings are everywhere */
else if (l2 > l1) return NULL; /* avoids a negative `l1' */
else {
const char *init; /* to search for a `*s2' inside `s1' */
l2--; /* 1st char will be checked by `memchr' */
l1 = l1-l2; /* `s2' cannot be found after that */
while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
init++; /* 1st char is already checked */
if (memcmp(init, s2+1, l2) == 0)
return init-1;
else { /* correct `l1' and `s1' to try again */
l1 -= init-s1;
s1 = init;
}
}
return NULL; /* not found */
}
}
bool str_find( const char* s, const char* p, int pos = 1 )
{
size_t ls,lp;
ls = strlen( s );
lp = strlen( p );
size_t init = posrelat( pos, ls );
if (init <1) init &#61; 1;
else if (init > ls &#43; 1) { /* start after string&#39;s end? */
//lua_pushnil(L); /* cannot find anything */
return false;
}
const char *s2 &#61; lmemfind(s &#43; init - 1, ls - init &#43; 1, p, lp);
if ( s2 )
{
size_t pa &#61; s2 - s &#43; 1;
size_t pb &#61; s2 - s &#43; lp;
return true;
}
//if (s2) {
//lua_pushinteger(L, s2 - s &#43; 1);
//lua_pushinteger(L, s2 - s &#43; lp);
//return 2;
//}
return false;
}
static void push_onecapture (MatchState *ms, int i, const char *s, const char *e)
{
if (i >&#61; ms->level)
{
if (i &#61;&#61; 0) /* ms->level &#61;&#61; 0, too */
{
//lua_pushlstring(ms->L, s, e - s); /* add whole match */
}
else
printf("invalid capture index");
}
else
{
ptrdiff_t l &#61; ms->capture[i].len;
if (l &#61;&#61; CAP_UNFINISHED)
printf("unfinished capture");
if (l &#61;&#61; CAP_POSITION)
{
//lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init &#43; 1);
}
else
{
//lua_pushlstring(ms->L, ms->capture[i].init, l);
}
}
}
std::string str_gsub( const char* src, const char*p, const char* ne, int max_ &#61; 0 )
{
size_t srcl, lp;
srcl &#61; strlen(src);
lp &#61; strlen(p);
int anchor &#61; (*p &#61;&#61; &#39;^&#39;);
if (anchor) {
p&#43;&#43;; lp--;
}
size_t max_s;
if ( max_ &#61;&#61; 0 )
max_s &#61; srcl &#43; 1;
else
max_s &#61; max_;
size_t n &#61; 0;
MatchState ms;
ms.matchdepth &#61; MAXCCALLS;
ms.src_init &#61; src;
ms.src_end &#61; src&#43;srcl;
ms.p_end &#61; p &#43; lp;
std::string strb;
while (n
{
const char *e;
ms.level &#61; 0;
//lua_assert(ms.matchdepth &#61;&#61; MAXCCALLS);
e &#61; match(&ms, src, p);
if (e) {
n&#43;&#43;;
//add_value(&ms, &b, src, e, tr);
size_t l, i;
const char *news &#61; ne;
l &#61; strlen( news );
for (i &#61; 0; i
if (news[i] !&#61; L_ESC)
//luaL_addchar(b, news[i]);
strb &#43;&#61; news[i];
else {
i&#43;&#43;; /* skip ESC */
if (!isdigit(uchar(news[i]))) {
if (news[i] !&#61; L_ESC)
printf("invalid use of in replacement string");
//luaL_addchar(b, news[i]);
strb &#43;&#61; news[i];
}
else if (news[i] &#61;&#61; &#39;0&#39;)
{
std::string str &#61; src;
str &#61; str.substr( e - src, str.size() - ( e-src ) );
strb.append( str );
//luaL_addlstring(b, s, e - s);
}
else {
push_onecapture(&ms, news[i] - &#39;1&#39;, src, e);
//luaL_addvalue(b); /* add capture to accumulated result */
}
}
}
}
if (e && e>src)
src &#61; e;
else if (src
{
//luaL_addchar(&b, *src&#43;&#43;);
strb &#43;&#61; *src&#43;&#43;;
}
else break;
if (anchor) break;
}
return strb;
}