[Python-checkins] r46173 - python/trunk/Objects/unicodeobject.c

Neal Norwitz nnorwitz at gmail.com
Thu May 25 09:12:41 CEST 2006


Since fastcount() can return i which is a Py_ssize_t, fastcount()
should return Py_ssize_t rather than int.  Also, count needs to be a
Py_ssize_t.

See below for another comment.

On 5/24/06, fredrik.lundh <python-checkins at python.org> wrote:
> Author: fredrik.lundh
> Date: Wed May 24 16:28:11 2006
> New Revision: 46173
>
>
> Modified: python/trunk/Objects/unicodeobject.c
> ==============================================================================
> --- python/trunk/Objects/unicodeobject.c        (original)
> +++ python/trunk/Objects/unicodeobject.c        Wed May 24 16:28:11 2006
> @@ -3848,7 +3848,94 @@
>
>  /* --- Helpers ------------------------------------------------------------ */
>
> -static Py_ssize_t count(PyUnicodeObject *self,
> +#define USE_FAST /* experimental fast search implementation */
> +
> +/* fast search/count implementation, based on a mix between boyer-
> +   moore and horspool, with a few more bells and whistles on the top.
> +   for some more background, see: http://effbot.org/stringlib */
> +
> +#define FAST_COUNT 0
> +#define FAST_SEARCH 1
> +
> +LOCAL(int) fastsearch(Py_UNICODE* s, Py_ssize_t n,
> +                      Py_UNICODE* p, Py_ssize_t m,
> +                      int mode)

Should be static?
Return Py_ssize_t instead of int?

> +{
> +    long mask;
> +    int skip, count = 0;

count should be Py_ssize_t, also no need to init here, though it's
probably not worth removing since it adds code below.

> +    Py_ssize_t i, j, mlast, w;

> +    w = n - m;
> +
> +    if (w < 0)
> +        return -1;

Move init of w and check below special case.  Also, if we are doing a
count, do we really want to return -1?  u''.count(u'a') isn't an
error.  I'm not sure how the rest of things are handled, so maybe this
is ok.

> +    /* look for special cases */
> +    if (m <= 1) {

m == 1 per amk's mail?

> +        if (m < 0)
> +            return -1;

If the real check is for m == 1, this check for m < 0 is not necessary.

> +        /* use special case for 1-character strings */
> +        if (mode == FAST_COUNT) {

 count = 0;  // if not init above

> +            for (i = 0; i < n; i++)
> +                if (s[i] == p[0])
> +                    count++;
> +            return count;
> +        } else {
> +            for (i = 0; i < n; i++)
> +                if (s[i] == p[0])
> +                    return i;
> +        }
> +        return -1;
> +    }

set w here and check for < 0.

count = 0;  // if not init above

+            if (!(mask & (1 << (s[i+m] & 0x1F))))
+                i = i + m;
+            else {
+                i = i + skip;
+                continue;

continue isn't necessary here.

Sorry, it's late and I realize this msg is kinda cryptic.  Hope you
can decipher.

n


More information about the Python-checkins mailing list