Fix Perl_swash_init & Perl_swash_fetch to save ERRSV (= $@)
[p5sagit/p5-mst-13.2.git] / regexec.c
index 1fa26c9..699ba4c 100644 (file)
--- a/regexec.c
+++ b/regexec.c
 #define HOPc(pos,off) ((char*)HOP(pos,off))
 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
 
+#define HOPBACK(pos, off) (            \
+    (UTF && DO_UTF8(PL_reg_sv))                \
+       ? reghopmaybe((U8*)pos, -off)   \
+    : (pos - off >= PL_bostr)          \
+       ? (U8*)(pos - off)              \
+    : (U8*)NULL                                \
+)
+#define HOPBACKc(pos, off) (char*)HOPBACK(pos, off)
+
 #define reghop3_c(pos,off,lim) ((char*)reghop3((U8*)pos, off, (U8*)lim))
 #define reghopmaybe3_c(pos,off,lim) ((char*)reghopmaybe3((U8*)pos, off, (U8*)lim))
 #define HOP3(pos,off,lim) (DO_UTF8(PL_reg_sv) ? reghop3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
 #define HOPMAYBE3c(pos,off,lim) ((char*)HOPMAYBE3(pos,off,lim))
 
-static void restore_pos(pTHXo_ void *arg);
+#define LOAD_UTF8_CHARCLASS(a,b) STMT_START { if (!CAT2(PL_utf8_,a)) (void)CAT2(is_utf8_, a)((U8*)b); } STMT_END
 
+static void restore_pos(pTHXo_ void *arg);
 
 STATIC CHECKPOINT
 S_regcppush(pTHX_ I32 parenfloor)
@@ -134,6 +144,9 @@ S_regcppush(pTHX_ I32 parenfloor)
     int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
     int p;
 
+    if (paren_elems_to_push < 0)
+       Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
+
 #define REGCP_OTHER_ELEMS 5
     SSCHECK(paren_elems_to_push + REGCP_OTHER_ELEMS);
     for (p = PL_regsize; p > parenfloor; p--) {
@@ -391,7 +404,8 @@ Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
                          && !PL_multiline ) ); /* Check after \n? */
 
        if (!ml_anch) {
-         if ( !(prog->reganch & ROPT_ANCH_GPOS) /* Checked by the caller */
+         if ( !(prog->reganch & (ROPT_ANCH_GPOS /* Checked by the caller */
+                                 | ROPT_IMPLICIT)) /* not a real BOL */
               /* SvCUR is not set on references: SvRV and SvPVX overlap */
               && sv && !SvROK(sv)
               && (strpos != strbeg)) {
@@ -944,18 +958,19 @@ S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *sta
            /* FALL THROUGH */
        case BOUND:
            if (do_utf8) {
-               if (s == startpos)
+               if (s == PL_bostr)
                    tmp = '\n';
                else {
                    U8 *r = reghop3((U8*)s, -1, (U8*)startpos);
                
-                   tmp = (I32)utf8n_to_uvuni(r, s - (char*)r, 0, 0);
+                   tmp = (I32)utf8n_to_uvchr(r, s - (char*)r, 0, 0);
                }
                tmp = ((OP(c) == BOUND ?
                        isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
+               LOAD_UTF8_CHARCLASS(alnum,"a");
                while (s < strend) {
                    if (tmp == !(OP(c) == BOUND ?
-                                swash_fetch(PL_utf8_alnum, (U8*)s) :
+                                swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
                                 isALNUM_LC_utf8((U8*)s)))
                    {
                        tmp = !tmp;
@@ -966,7 +981,7 @@ S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *sta
                }
            }
            else {
-               tmp = (s != startpos) ? UCHARAT(s - 1) : '\n';
+               tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
                tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
                while (s < strend) {
                    if (tmp ==
@@ -986,18 +1001,19 @@ S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *sta
            /* FALL THROUGH */
        case NBOUND:
            if (do_utf8) {
-               if (s == startpos)
+               if (s == PL_bostr)
                    tmp = '\n';
                else {
                    U8 *r = reghop3((U8*)s, -1, (U8*)startpos);
                
-                   tmp = (I32)utf8n_to_uvuni(r, s - (char*)r, 0, 0);
+                   tmp = (I32)utf8n_to_uvchr(r, s - (char*)r, 0, 0);
                }
                tmp = ((OP(c) == NBOUND ?
                        isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
+               LOAD_UTF8_CHARCLASS(alnum,"a");
                while (s < strend) {
                    if (tmp == !(OP(c) == NBOUND ?
-                                swash_fetch(PL_utf8_alnum, (U8*)s) :
+                                swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
                                 isALNUM_LC_utf8((U8*)s)))
                        tmp = !tmp;
                    else if ((norun || regtry(prog, s)))
@@ -1006,7 +1022,7 @@ S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *sta
                }
            }
            else {
-               tmp = (s != startpos) ? UCHARAT(s - 1) : '\n';
+               tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
                tmp = ((OP(c) == NBOUND ?
                        isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
                while (s < strend) {
@@ -1023,8 +1039,9 @@ S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *sta
            break;
        case ALNUM:
            if (do_utf8) {
+               LOAD_UTF8_CHARCLASS(alnum,"a");
                while (s < strend) {
-                   if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
+                   if (swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
                        if (tmp && (norun || regtry(prog, s)))
                            goto got_it;
                        else
@@ -1080,8 +1097,9 @@ S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *sta
            break;
        case NALNUM:
            if (do_utf8) {
+               LOAD_UTF8_CHARCLASS(alnum,"a");
                while (s < strend) {
-                   if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
+                   if (!swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
                        if (tmp && (norun || regtry(prog, s)))
                            goto got_it;
                        else
@@ -1137,8 +1155,9 @@ S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *sta
            break;
        case SPACE:
            if (do_utf8) {
+               LOAD_UTF8_CHARCLASS(space," ");
                while (s < strend) {
-                   if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
+                   if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)) {
                        if (tmp && (norun || regtry(prog, s)))
                            goto got_it;
                        else
@@ -1194,8 +1213,9 @@ S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *sta
            break;
        case NSPACE:
            if (do_utf8) {
+               LOAD_UTF8_CHARCLASS(space," ");
                while (s < strend) {
-                   if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
+                   if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8))) {
                        if (tmp && (norun || regtry(prog, s)))
                            goto got_it;
                        else
@@ -1251,8 +1271,9 @@ S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *sta
            break;
        case DIGIT:
            if (do_utf8) {
+               LOAD_UTF8_CHARCLASS(digit,"0");
                while (s < strend) {
-                   if (swash_fetch(PL_utf8_digit,(U8*)s)) {
+                   if (swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
                        if (tmp && (norun || regtry(prog, s)))
                            goto got_it;
                        else
@@ -1308,8 +1329,9 @@ S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *sta
            break;
        case NDIGIT:
            if (do_utf8) {
+               LOAD_UTF8_CHARCLASS(digit,"0");
                while (s < strend) {
-                   if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
+                   if (!swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
                        if (tmp && (norun || regtry(prog, s)))
                            goto got_it;
                        else
@@ -1419,19 +1441,6 @@ Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *
       if (strend - startpos < minlen) goto phooey;
     }
 
-    if (startpos == strbeg)    /* is ^ valid at stringarg? */
-       PL_regprev = '\n';
-    else {
-        if (prog->reganch & ROPT_UTF8 && do_utf8) {
-           U8 *s = reghop3((U8*)stringarg, -1, (U8*)strbeg);
-           PL_regprev = utf8n_to_uvuni(s, (U8*)stringarg - s, NULL, 0);
-       }
-       else
-           PL_regprev = (U32)stringarg[-1];
-       if (!PL_multiline && PL_regprev == '\n')
-           PL_regprev = '\0';          /* force ^ to NOT match */
-    }
-
     /* Check validity of program. */
     if (UCHARAT(prog->program) != REG_MAGIC) {
        Perl_croak(aTHX_ "corrupted regexp program");
@@ -1468,7 +1477,8 @@ Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *
            PL_reg_ganch = startpos;
        else if (sv && SvTYPE(sv) >= SVt_PVMG
                  && SvMAGIC(sv)
-                 && (mg = mg_find(sv, 'g')) && mg->mg_len >= 0) {
+                 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
+                 && mg->mg_len >= 0) {
            PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
            if (prog->reganch & ROPT_ANCH_GPOS) {
                if (s > PL_reg_ganch)
@@ -1819,10 +1829,11 @@ S_regtry(pTHX_ regexp *prog, char *startpos)
            }
        
            if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
-                 && (mg = mg_find(PL_reg_sv, 'g')))) {
+                 && (mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global)))) {
                /* prepare for quick setting of pos */
-               sv_magic(PL_reg_sv, (SV*)0, 'g', Nullch, 0);
-               mg = mg_find(PL_reg_sv, 'g');
+               sv_magic(PL_reg_sv, (SV*)0,
+                       PERL_MAGIC_regex_global, Nullch, 0);
+               mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global);
                mg->mg_len = -1;
            }
            PL_reg_magic    = mg;
@@ -2034,19 +2045,16 @@ S_regmatch(pTHX_ regnode *prog)
 
        switch (OP(scan)) {
        case BOL:
-           if (locinput == PL_bostr
-               ? PL_regprev == '\n'
-               : (PL_multiline &&
-                  (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
+           if (locinput == PL_bostr || (PL_multiline &&
+               (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
            {
                /* regtill = regbol; */
                break;
            }
            sayNO;
        case MBOL:
-           if (locinput == PL_bostr
-               ? PL_regprev == '\n'
-               : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
+           if (locinput == PL_bostr ||
+               ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
            {
                break;
            }
@@ -2152,7 +2160,7 @@ S_regmatch(pTHX_ regnode *prog)
                    if (l >= PL_regeol) {
                        sayNO;
                    }
-                   if ((UTF ? utf8n_to_uvuni((U8*)s, e - s, 0, 0) : *((U8*)s)) !=
+                   if ((UTF ? utf8n_to_uvchr((U8*)s, e - s, 0, 0) : *((U8*)s)) !=
                        (c1 ? toLOWER_utf8((U8*)l) : toLOWER_LC_utf8((U8*)l)))
                            sayNO;
                    s += UTF ? UTF8SKIP(s) : 1;
@@ -2203,8 +2211,9 @@ S_regmatch(pTHX_ regnode *prog)
            if (!nextchr)
                sayNO;
            if (do_utf8) {
+               LOAD_UTF8_CHARCLASS(alnum,"a");
                if (!(OP(scan) == ALNUM
-                     ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
+                     ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
                      : isALNUM_LC_utf8((U8*)locinput)))
                {
                    sayNO;
@@ -2225,8 +2234,9 @@ S_regmatch(pTHX_ regnode *prog)
            if (!nextchr && locinput >= PL_regeol)
                sayNO;
            if (do_utf8) {
+               LOAD_UTF8_CHARCLASS(alnum,"a");
                if (OP(scan) == NALNUM
-                   ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
+                   ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
                    : isALNUM_LC_utf8((U8*)locinput))
                {
                    sayNO;
@@ -2248,16 +2258,17 @@ S_regmatch(pTHX_ regnode *prog)
        case NBOUND:
            /* was last char in word? */
            if (do_utf8) {
-               if (locinput == PL_regbol)
-                   ln = PL_regprev;
+               if (locinput == PL_bostr)
+                   ln = '\n';
                else {
                    U8 *r = reghop((U8*)locinput, -1);
                
-                   ln = utf8n_to_uvuni(r, s - (char*)r, 0, 0);
+                   ln = utf8n_to_uvchr(r, s - (char*)r, 0, 0);
                }
                if (OP(scan) == BOUND || OP(scan) == NBOUND) {
                    ln = isALNUM_uni(ln);
-                   n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
+                   LOAD_UTF8_CHARCLASS(alnum,"a");
+                   n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
                }
                else {
                    ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
@@ -2265,8 +2276,8 @@ S_regmatch(pTHX_ regnode *prog)
                }
            }
            else {
-               ln = (locinput != PL_regbol) ?
-                   UCHARAT(locinput - 1) : PL_regprev;
+               ln = (locinput != PL_bostr) ?
+                   UCHARAT(locinput - 1) : '\n';
                if (OP(scan) == BOUND || OP(scan) == NBOUND) {
                    ln = isALNUM(ln);
                    n = isALNUM(nextchr);
@@ -2288,8 +2299,9 @@ S_regmatch(pTHX_ regnode *prog)
                sayNO;
            if (do_utf8) {
                if (UTF8_IS_CONTINUED(nextchr)) {
+                   LOAD_UTF8_CHARCLASS(space," ");
                    if (!(OP(scan) == SPACE
-                         ? swash_fetch(PL_utf8_space, (U8*)locinput)
+                         ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
                          : isSPACE_LC_utf8((U8*)locinput)))
                    {
                        sayNO;
@@ -2317,8 +2329,9 @@ S_regmatch(pTHX_ regnode *prog)
            if (!nextchr && locinput >= PL_regeol)
                sayNO;
            if (do_utf8) {
+               LOAD_UTF8_CHARCLASS(space," ");
                if (OP(scan) == NSPACE
-                   ? swash_fetch(PL_utf8_space, (U8*)locinput)
+                   ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
                    : isSPACE_LC_utf8((U8*)locinput))
                {
                    sayNO;
@@ -2339,8 +2352,9 @@ S_regmatch(pTHX_ regnode *prog)
            if (!nextchr)
                sayNO;
            if (do_utf8) {
+               LOAD_UTF8_CHARCLASS(digit,"0");
                if (!(OP(scan) == DIGIT
-                     ? swash_fetch(PL_utf8_digit, (U8*)locinput)
+                     ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
                      : isDIGIT_LC_utf8((U8*)locinput)))
                {
                    sayNO;
@@ -2361,8 +2375,9 @@ S_regmatch(pTHX_ regnode *prog)
            if (!nextchr && locinput >= PL_regeol)
                sayNO;
            if (do_utf8) {
+               LOAD_UTF8_CHARCLASS(digit,"0");
                if (OP(scan) == NDIGIT
-                   ? swash_fetch(PL_utf8_digit, (U8*)locinput)
+                   ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
                    : isDIGIT_LC_utf8((U8*)locinput))
                {
                    sayNO;
@@ -2377,10 +2392,13 @@ S_regmatch(pTHX_ regnode *prog)
            nextchr = UCHARAT(++locinput);
            break;
        case CLUMP:
-           if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
+           LOAD_UTF8_CHARCLASS(mark,"~");
+           if (locinput >= PL_regeol ||
+               swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
                sayNO;
            locinput += PL_utf8skip[nextchr];
-           while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
+           while (locinput < PL_regeol &&
+                  swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
                locinput += UTF8SKIP(locinput);
            if (locinput > PL_regeol)
                sayNO;
@@ -2490,7 +2508,7 @@ S_regmatch(pTHX_ regnode *prog)
                        SV *sv = SvROK(ret) ? SvRV(ret) : ret;
 
                        if(SvMAGICAL(sv))
-                           mg = mg_find(sv, 'r');
+                           mg = mg_find(sv, PERL_MAGIC_qr);
                    }
                    if (mg) {
                        re = (regexp *)mg->mg_obj;
@@ -2504,11 +2522,12 @@ S_regmatch(pTHX_ regnode *prog)
                        I32 osize = PL_regsize;
                        I32 onpar = PL_regnpar;
 
-                       pm.op_pmflags = 0;
+                       Zero(&pm, 1, PMOP);
                        re = CALLREGCOMP(aTHX_ t, t + len, &pm);
                        if (!(SvFLAGS(ret)
                              & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
-                           sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
+                           sv_magic(ret,(SV*)ReREFCNT_inc(re),
+                                       PERL_MAGIC_qr,0,0);
                        PL_regprecomp = oprecomp;
                        PL_regsize = osize;
                        PL_regnpar = onpar;
@@ -3229,9 +3248,15 @@ S_regmatch(pTHX_ regnode *prog)
                            c = utf8_to_uvchr((U8*)PL_reginput, NULL);
                        else
                            c = UCHARAT(PL_reginput);
+                       /* If it could work, try it. */
+                       if (c == c1 || c == c2)
+                       {
+                           TRYPAREN(paren, n, PL_reginput);
+                           REGCP_UNWIND(lastcp);
+                       }
                    }
                    /* If it could work, try it. */
-                   if (c1 == -1000 || c == c1 || c == c2)
+                   else if (c1 == -1000)
                    {
                        TRYPAREN(paren, n, PL_reginput);
                        REGCP_UNWIND(lastcp);
@@ -3359,20 +3384,10 @@ S_regmatch(pTHX_ regnode *prog)
        case UNLESSM:
            n = 0;
            if (scan->flags) {
-               if (UTF) {              /* XXXX This is absolutely
-                                          broken, we read before
-                                          start of string. */
-                   s = HOPMAYBEc(locinput, -scan->flags);
-                   if (!s)
-                       goto say_yes;
-                   PL_reginput = s;
-               }
-               else {
-                   if (locinput < PL_bostr + scan->flags)
-                       goto say_yes;
-                   PL_reginput = locinput - scan->flags;
-                   goto do_ifmatch;
-               }
+               s = HOPBACKc(locinput, scan->flags);
+               if (!s)
+                   goto say_yes;
+               PL_reginput = s;
            }
            else
                PL_reginput = locinput;
@@ -3380,20 +3395,10 @@ S_regmatch(pTHX_ regnode *prog)
        case IFMATCH:
            n = 1;
            if (scan->flags) {
-               if (UTF) {              /* XXXX This is absolutely
-                                          broken, we read before
-                                          start of string. */
-                   s = HOPMAYBEc(locinput, -scan->flags);
-                   if (!s || s < PL_bostr)
-                       goto say_no;
-                   PL_reginput = s;
-               }
-               else {
-                   if (locinput < PL_bostr + scan->flags)
-                       goto say_no;
-                   PL_reginput = locinput - scan->flags;
-                   goto do_ifmatch;
-               }
+               s = HOPBACKc(locinput, scan->flags);
+               if (!s)
+                   goto say_no;
+               PL_reginput = s;
            }
            else
                PL_reginput = locinput;
@@ -3598,8 +3603,9 @@ S_regrepeat(pTHX_ regnode *p, I32 max)
     case ALNUM:
        if (do_utf8) {
            loceol = PL_regeol;
+           LOAD_UTF8_CHARCLASS(alnum,"a");
            while (hardcount < max && scan < loceol &&
-                  swash_fetch(PL_utf8_alnum, (U8*)scan)) {
+                  swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
                scan += UTF8SKIP(scan);
                hardcount++;
            }
@@ -3625,8 +3631,9 @@ S_regrepeat(pTHX_ regnode *p, I32 max)
     case NALNUM:
        if (do_utf8) {
            loceol = PL_regeol;
+           LOAD_UTF8_CHARCLASS(alnum,"a");
            while (hardcount < max && scan < loceol &&
-                  !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
+                  !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
                scan += UTF8SKIP(scan);
                hardcount++;
            }
@@ -3652,8 +3659,10 @@ S_regrepeat(pTHX_ regnode *p, I32 max)
     case SPACE:
        if (do_utf8) {
            loceol = PL_regeol;
+           LOAD_UTF8_CHARCLASS(space," ");
            while (hardcount < max && scan < loceol &&
-                  (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
+                  (*scan == ' ' ||
+                   swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
                scan += UTF8SKIP(scan);
                hardcount++;
            }
@@ -3679,8 +3688,10 @@ S_regrepeat(pTHX_ regnode *p, I32 max)
     case NSPACE:
        if (do_utf8) {
            loceol = PL_regeol;
+           LOAD_UTF8_CHARCLASS(space," ");
            while (hardcount < max && scan < loceol &&
-                  !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
+                  !(*scan == ' ' ||
+                    swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
                scan += UTF8SKIP(scan);
                hardcount++;
            }
@@ -3706,8 +3717,9 @@ S_regrepeat(pTHX_ regnode *p, I32 max)
     case DIGIT:
        if (do_utf8) {
            loceol = PL_regeol;
+           LOAD_UTF8_CHARCLASS(digit,"0");
            while (hardcount < max && scan < loceol &&
-                  swash_fetch(PL_utf8_digit,(U8*)scan)) {
+                  swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
                scan += UTF8SKIP(scan);
                hardcount++;
            }
@@ -3719,8 +3731,9 @@ S_regrepeat(pTHX_ regnode *p, I32 max)
     case NDIGIT:
        if (do_utf8) {
            loceol = PL_regeol;
+           LOAD_UTF8_CHARCLASS(digit,"0");
            while (hardcount < max && scan < loceol &&
-                  !swash_fetch(PL_utf8_digit,(U8*)scan)) {
+                  !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
                scan += UTF8SKIP(scan);
                hardcount++;
            }
@@ -3850,25 +3863,22 @@ S_reginclass(pTHX_ register regnode *n, register U8* p, register bool do_utf8)
     char flags = ANYOF_FLAGS(n);
     bool match = FALSE;
     UV c;
-    STRLEN len;
+    STRLEN len = 0;
 
-    if (do_utf8)
-       c = utf8_to_uvchr(p, &len);
-    else
-       c = *p;
+    c = do_utf8 ? utf8_to_uvchr(p, &len) : *p;
 
     if (do_utf8 || (flags & ANYOF_UNICODE)) {
        if (do_utf8 && !ANYOF_RUNTIME(n)) {
            if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
                match = TRUE;
        }
-       if (do_utf8 && flags & ANYOF_UNICODE_ALL && c >= 256)
+       if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
            match = TRUE;
        if (!match) {
            SV *sw = regclass_swash(n, TRUE, 0);
        
            if (sw) {
-               if (swash_fetch(sw, p))
+               if (swash_fetch(sw, p, do_utf8))
                    match = TRUE;
                else if (flags & ANYOF_FOLD) {
                    U8 tmpbuf[UTF8_MAXLEN+1];
@@ -3879,7 +3889,7 @@ S_reginclass(pTHX_ register regnode *n, register U8* p, register bool do_utf8)
                    }
                    else
                        uvchr_to_utf8(tmpbuf, toLOWER_utf8(p));
-                   if (swash_fetch(sw, tmpbuf))
+                   if (swash_fetch(sw, tmpbuf, do_utf8))
                        match = TRUE;
                }
            }
@@ -3889,7 +3899,7 @@ S_reginclass(pTHX_ register regnode *n, register U8* p, register bool do_utf8)
        if (ANYOF_BITMAP_TEST(n, c))
            match = TRUE;
        else if (flags & ANYOF_FOLD) {
-           I32 f;
+         I32 f;
 
            if (flags & ANYOF_LOCALE) {
                PL_reg_flags |= RF_tainted;