LCOV - code coverage report
Current view: top level - Objects/stringlib - partition.h (source / functions) Hit Total Coverage
Test: CPython lcov report Lines: 56 66 84.8 %
Date: 2022-07-07 18:19:46 Functions: 10 10 100.0 %

          Line data    Source code
       1             : /* stringlib: partition implementation */
       2             : 
       3             : #ifndef STRINGLIB_FASTSEARCH_H
       4             : #  error must include "stringlib/fastsearch.h" before including this module
       5             : #endif
       6             : 
       7             : #if !STRINGLIB_MUTABLE && !defined(STRINGLIB_GET_EMPTY)
       8             : #  error "STRINGLIB_GET_EMPTY must be defined if STRINGLIB_MUTABLE is zero"
       9             : #endif
      10             : 
      11             : 
      12             : Py_LOCAL_INLINE(PyObject*)
      13      161271 : STRINGLIB(partition)(PyObject* str_obj,
      14             :                     const STRINGLIB_CHAR* str, Py_ssize_t str_len,
      15             :                     PyObject* sep_obj,
      16             :                     const STRINGLIB_CHAR* sep, Py_ssize_t sep_len)
      17             : {
      18             :     PyObject* out;
      19             :     Py_ssize_t pos;
      20             : 
      21      161271 :     if (sep_len == 0) {
      22           2 :         PyErr_SetString(PyExc_ValueError, "empty separator");
      23           2 :         return NULL;
      24             :     }
      25             : 
      26      161269 :     out = PyTuple_New(3);
      27      161269 :     if (!out)
      28           0 :         return NULL;
      29             : 
      30      161269 :     pos = FASTSEARCH(str, str_len, sep, sep_len, -1, FAST_SEARCH);
      31             : 
      32      161269 :     if (pos < 0) {
      33             : #if STRINGLIB_MUTABLE
      34           4 :         PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, str_len));
      35           4 :         PyTuple_SET_ITEM(out, 1, STRINGLIB_NEW(NULL, 0));
      36           4 :         PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(NULL, 0));
      37             : 
      38           4 :         if (PyErr_Occurred()) {
      39           0 :             Py_DECREF(out);
      40           0 :             return NULL;
      41             :         }
      42             : #else
      43       38204 :         Py_INCREF(str_obj);
      44       38204 :         PyTuple_SET_ITEM(out, 0, (PyObject*) str_obj);
      45       38204 :         PyObject *empty = (PyObject*)STRINGLIB_GET_EMPTY();
      46       38204 :         assert(empty != NULL);
      47       38204 :         Py_INCREF(empty);
      48       38204 :         PyTuple_SET_ITEM(out, 1, empty);
      49       38204 :         Py_INCREF(empty);
      50       38204 :         PyTuple_SET_ITEM(out, 2, empty);
      51             : #endif
      52       38208 :         return out;
      53             :     }
      54             : 
      55      123061 :     PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos));
      56      123061 :     Py_INCREF(sep_obj);
      57      123061 :     PyTuple_SET_ITEM(out, 1, sep_obj);
      58      123061 :     pos += sep_len;
      59      123061 :     PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos));
      60             : 
      61      123061 :     if (PyErr_Occurred()) {
      62           0 :         Py_DECREF(out);
      63           0 :         return NULL;
      64             :     }
      65             : 
      66      123061 :     return out;
      67             : }
      68             : 
      69             : Py_LOCAL_INLINE(PyObject*)
      70     1735955 : STRINGLIB(rpartition)(PyObject* str_obj,
      71             :                      const STRINGLIB_CHAR* str, Py_ssize_t str_len,
      72             :                      PyObject* sep_obj,
      73             :                      const STRINGLIB_CHAR* sep, Py_ssize_t sep_len)
      74             : {
      75             :     PyObject* out;
      76             :     Py_ssize_t pos;
      77             : 
      78     1735955 :     if (sep_len == 0) {
      79           2 :         PyErr_SetString(PyExc_ValueError, "empty separator");
      80           2 :         return NULL;
      81             :     }
      82             : 
      83     1735955 :     out = PyTuple_New(3);
      84     1735955 :     if (!out)
      85           0 :         return NULL;
      86             : 
      87     1735955 :     pos = FASTSEARCH(str, str_len, sep, sep_len, -1, FAST_RSEARCH);
      88             : 
      89     1735955 :     if (pos < 0) {
      90             : #if STRINGLIB_MUTABLE
      91           4 :         PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(NULL, 0));
      92           4 :         PyTuple_SET_ITEM(out, 1, STRINGLIB_NEW(NULL, 0));
      93           4 :         PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str, str_len));
      94             : 
      95           4 :         if (PyErr_Occurred()) {
      96           0 :             Py_DECREF(out);
      97           0 :             return NULL;
      98             :         }
      99             : #else
     100      845267 :         PyObject *empty = (PyObject*)STRINGLIB_GET_EMPTY();
     101      845267 :         assert(empty != NULL);
     102      845267 :         Py_INCREF(empty);
     103      845267 :         PyTuple_SET_ITEM(out, 0, empty);
     104      845267 :         Py_INCREF(empty);
     105      845267 :         PyTuple_SET_ITEM(out, 1, empty);
     106      845267 :         Py_INCREF(str_obj);
     107      845267 :         PyTuple_SET_ITEM(out, 2, (PyObject*) str_obj);
     108             : #endif
     109      845271 :         return out;
     110             :     }
     111             : 
     112      890684 :     PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos));
     113      890684 :     Py_INCREF(sep_obj);
     114      890684 :     PyTuple_SET_ITEM(out, 1, sep_obj);
     115      890684 :     pos += sep_len;
     116      890684 :     PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos));
     117             : 
     118      890684 :     if (PyErr_Occurred()) {
     119           0 :         Py_DECREF(out);
     120           0 :         return NULL;
     121             :     }
     122             : 
     123      890684 :     return out;
     124             : }
     125             : 

Generated by: LCOV version 1.14