Repository: numpy/numpy
File: numpy/_core/src/umath/stringdtype_ufuncs.cpp (C++)

Code before:
            while (inbuf_ptr < inbuf_ptr_end && *(inbuf_ptr_end - 1) == 0) {
                inbuf_ptr_end--;
            }

            while (inbuf_ptr < inbuf_ptr_end) {
                num_codepoints++;
                int num_bytes = num_bytes_for_utf8_character(
                        ((unsigned char *)inbuf_ptr));
                codepoint_offsets.push_back((unsigned char *)inbuf_ptr);
                inbuf_ptr += num_bytes;
            }
        }

        // adjust slice to string length in codepoints
        // and handle negative indices
        npy_intp slice_length =
                PySlice_AdjustIndices(num_codepoints, &start, &stop, step);

        if (step == 1) {
            // step == 1 is the easy case, we can just use memcpy
            npy_intp outsize = ((size_t)stop < num_codepoints
                                        ? codepoint_offsets[stop]
                                        : (unsigned char *)is.buf + is.size) -
                               ((size_t)start < num_codepoints
                                        ? codepoint_offsets[start]
                                        : (unsigned char *)is.buf + is.size);

            if (load_new_string(ops, &os, outsize, oallocator, "slice") < 0) {
                goto fail;
            }

            /* explicitly discard const; initializing new buffer */
            char *buf = (char *)os.buf;

            memcpy(buf, ((size_t)start < num_codepoints
                                 ? codepoint_offsets[start]
                                 : (unsigned char *)is.buf + is.size), outsize);
        }
        else {
            // handle step != 1
            // compute outsize
            npy_intp outsize = 0;
            for (int i = start; step > 0 ? i < stop : i > stop; i += step) {
                outsize += num_bytes_for_utf8_character(codepoint_offsets[i]);
            }

            if (outsize > 0) {
                if (load_new_string(ops, &os, outsize, oallocator, "slice") < 0) {
                    goto fail;
                }


Review comment (suggestion):
Makes me think we should just use `start_ptr = ...`, `stop_ptr =` and then calculate the `outside` from that (and re-use the `start_ptr` below).

Code after:
            while (inbuf_ptr < inbuf_ptr_end && *(inbuf_ptr_end - 1) == 0) {
                inbuf_ptr_end--;
            }

            while (inbuf_ptr < inbuf_ptr_end) {
                num_codepoints++;
                int num_bytes = num_bytes_for_utf8_character(
                        ((unsigned char *)inbuf_ptr));
                codepoint_offsets.push_back((unsigned char *)inbuf_ptr);
                inbuf_ptr += num_bytes;
            }
        }

        // adjust slice to string length in codepoints
        // and handle negative indices
        npy_intp slice_length =
                PySlice_AdjustIndices(num_codepoints, &start, &stop, step);

        if (step == 1) {
            // step == 1 is the easy case, we can just use memcpy
            unsigned char *start_bounded = ((size_t)start < num_codepoints
                                            ? codepoint_offsets[start]
                                            : (unsigned char *)is.buf + is.size);
            unsigned char *stop_bounded = ((size_t)stop < num_codepoints
                                           ? codepoint_offsets[stop]
                                           : (unsigned char *)is.buf + is.size);
            npy_intp outsize = stop_bounded - start_bounded;

            if (load_new_string(ops, &os, outsize, oallocator, "slice") < 0) {
                goto fail;
            }

            /* explicitly discard const; initializing new buffer */
            char *buf = (char *)os.buf;

            memcpy(buf, start_bounded, outsize);
        }
        else {
            // handle step != 1
            // compute outsize
            npy_intp outsize = 0;
            for (int i = start; step > 0 ? i < stop : i > stop; i += step) {
                outsize += num_bytes_for_utf8_character(codepoint_offsets[i]);
            }

            if (outsize > 0) {
                if (load_new_string(ops, &os, outsize, oallocator, "slice") < 0) {
                    goto fail;
                }

                /* explicitly discard const; initializing new buffer */