@@ -8964,21 +8964,20 @@ _PyUnicode_InsertThousandsGrouping(
8964
8964
return count ;
8965
8965
}
8966
8966
8967
-
8968
- Py_ssize_t
8969
- PyUnicode_Count (PyObject * str ,
8970
- PyObject * substr ,
8971
- Py_ssize_t start ,
8972
- Py_ssize_t end )
8967
+ static Py_ssize_t
8968
+ unicode_count_impl (PyObject * str ,
8969
+ PyObject * substr ,
8970
+ Py_ssize_t start ,
8971
+ Py_ssize_t end )
8973
8972
{
8973
+ assert (PyUnicode_Check (str ));
8974
+ assert (PyUnicode_Check (substr ));
8975
+
8974
8976
Py_ssize_t result ;
8975
8977
int kind1 , kind2 ;
8976
8978
const void * buf1 = NULL , * buf2 = NULL ;
8977
8979
Py_ssize_t len1 , len2 ;
8978
8980
8979
- if (ensure_unicode (str ) < 0 || ensure_unicode (substr ) < 0 )
8980
- return -1 ;
8981
-
8982
8981
kind1 = PyUnicode_KIND (str );
8983
8982
kind2 = PyUnicode_KIND (substr );
8984
8983
if (kind1 < kind2 )
@@ -8998,6 +8997,7 @@ PyUnicode_Count(PyObject *str,
8998
8997
goto onError ;
8999
8998
}
9000
8999
9000
+ // We don't reuse `anylib_count` here because of the explicit casts.
9001
9001
switch (kind1 ) {
9002
9002
case PyUnicode_1BYTE_KIND :
9003
9003
result = ucs1lib_count (
@@ -9033,6 +9033,18 @@ PyUnicode_Count(PyObject *str,
9033
9033
return -1 ;
9034
9034
}
9035
9035
9036
+ Py_ssize_t
9037
+ PyUnicode_Count (PyObject * str ,
9038
+ PyObject * substr ,
9039
+ Py_ssize_t start ,
9040
+ Py_ssize_t end )
9041
+ {
9042
+ if (ensure_unicode (str ) < 0 || ensure_unicode (substr ) < 0 )
9043
+ return -1 ;
9044
+
9045
+ return unicode_count_impl (str , substr , start , end );
9046
+ }
9047
+
9036
9048
Py_ssize_t
9037
9049
PyUnicode_Find (PyObject * str ,
9038
9050
PyObject * substr ,
@@ -10848,62 +10860,16 @@ unicode_count(PyObject *self, PyObject *args)
10848
10860
PyObject * substring = NULL ; /* initialize to fix a compiler warning */
10849
10861
Py_ssize_t start = 0 ;
10850
10862
Py_ssize_t end = PY_SSIZE_T_MAX ;
10851
- PyObject * result ;
10852
- int kind1 , kind2 ;
10853
- const void * buf1 , * buf2 ;
10854
- Py_ssize_t len1 , len2 , iresult ;
10863
+ Py_ssize_t result ;
10855
10864
10856
10865
if (!parse_args_finds_unicode ("count" , args , & substring , & start , & end ))
10857
10866
return NULL ;
10858
10867
10859
- kind1 = PyUnicode_KIND (self );
10860
- kind2 = PyUnicode_KIND (substring );
10861
- if (kind1 < kind2 )
10862
- return PyLong_FromLong (0 );
10863
-
10864
- len1 = PyUnicode_GET_LENGTH (self );
10865
- len2 = PyUnicode_GET_LENGTH (substring );
10866
- ADJUST_INDICES (start , end , len1 );
10867
- if (end - start < len2 )
10868
- return PyLong_FromLong (0 );
10869
-
10870
- buf1 = PyUnicode_DATA (self );
10871
- buf2 = PyUnicode_DATA (substring );
10872
- if (kind2 != kind1 ) {
10873
- buf2 = unicode_askind (kind2 , buf2 , len2 , kind1 );
10874
- if (!buf2 )
10875
- return NULL ;
10876
- }
10877
- switch (kind1 ) {
10878
- case PyUnicode_1BYTE_KIND :
10879
- iresult = ucs1lib_count (
10880
- ((const Py_UCS1 * )buf1 ) + start , end - start ,
10881
- buf2 , len2 , PY_SSIZE_T_MAX
10882
- );
10883
- break ;
10884
- case PyUnicode_2BYTE_KIND :
10885
- iresult = ucs2lib_count (
10886
- ((const Py_UCS2 * )buf1 ) + start , end - start ,
10887
- buf2 , len2 , PY_SSIZE_T_MAX
10888
- );
10889
- break ;
10890
- case PyUnicode_4BYTE_KIND :
10891
- iresult = ucs4lib_count (
10892
- ((const Py_UCS4 * )buf1 ) + start , end - start ,
10893
- buf2 , len2 , PY_SSIZE_T_MAX
10894
- );
10895
- break ;
10896
- default :
10897
- Py_UNREACHABLE ();
10898
- }
10899
-
10900
- result = PyLong_FromSsize_t (iresult );
10901
-
10902
- assert ((kind2 == kind1 ) == (buf2 == PyUnicode_DATA (substring )));
10903
- if (kind2 != kind1 )
10904
- PyMem_Free ((void * )buf2 );
10868
+ result = unicode_count_impl (self , substring , start , end );
10869
+ if (result == -1 )
10870
+ return NULL ;
10905
10871
10906
- return result ;
10872
+ return PyLong_FromSsize_t ( result ) ;
10907
10873
}
10908
10874
10909
10875
/*[clinic input]
0 commit comments