MCPcopy Create free account
hub / github.com/ashvardanian/StringZilla / Strs_in

Function Strs_in

python/stringzilla.c:2183–2210  ·  view source on GitHub ↗

* @brief Will be called by the `PySequence_Contains` to check the presence of a string in array. * @return 1 if the string is present, 0 if it is not, -1 in case of error. * @see Docs: https://docs.python.org/3/c-api/sequence.html#c.PySequence_Contains */

Source from the content-addressed store, hash-verified

2181 * @see Docs: https://docs.python.org/3/c-api/sequence.html#c.PySequence_Contains
2182 */
2183static int Strs_in(Str *self, PyObject *needle_obj) {
2184
2185 // Validate and convert `needle`
2186 sz_string_view_t needle;
2187 if (!sz_py_export_string_like(needle_obj, &needle.start, &needle.length)) {
2188 wrap_current_exception("The needle argument must be string-like");
2189 return -1;
2190 }
2191
2192 // Depending on the layout, we will need to use different logic
2193 Py_ssize_t count = Strs_len(self);
2194 get_string_at_offset_t getter = str_at_offset_getter(self);
2195 if (!getter) {
2196 PyErr_SetString(PyExc_TypeError, "Unknown Strs kind");
2197 return -1;
2198 }
2199
2200 // Time for a full-scan
2201 for (Py_ssize_t i = 0; i < count; ++i) {
2202 PyObject *parent = NULL;
2203 sz_cptr_t start = NULL;
2204 sz_size_t length = 0;
2205 getter(self, i, count, &parent, &start, &length);
2206 if (length == needle.length && sz_equal(start, needle.start, needle.length) == sz_true_k) return 1;
2207 }
2208
2209 return 0;
2210}
2211
2212static PyObject *Str_richcompare(PyObject *self, PyObject *other, int op) {
2213

Callers

nothing calls this directly

Calls 5

sz_py_export_string_likeFunction · 0.85
wrap_current_exceptionFunction · 0.85
Strs_lenFunction · 0.85
str_at_offset_getterFunction · 0.85
sz_equalFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…