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

Function Strs_init

python/stringzilla.c:7474–7539  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7472}
7473
7474static int Strs_init(Strs *self, PyObject *args, PyObject *kwargs) {
7475
7476 // Manual argument parsing for performance
7477 Py_ssize_t nargs = PyTuple_Size(args);
7478 if (nargs > 2) {
7479 PyErr_SetString(PyExc_TypeError,
7480 "Strs() takes at most 2 arguments: sequence of strings and a boolean indicator");
7481 return -1;
7482 }
7483
7484 PyObject *sequence_obj = nargs >= 1 ? PyTuple_GET_ITEM(args, 0) : NULL;
7485 PyObject *view_obj = nargs >= 2 ? PyTuple_GET_ITEM(args, 1) : NULL;
7486 int view = 0; // Default to copy mode
7487
7488 // Parse keyword arguments if provided
7489 if (kwargs) {
7490 Py_ssize_t pos = 0;
7491 PyObject *key, *value;
7492 while (PyDict_Next(kwargs, &pos, &key, &value)) {
7493 if (PyUnicode_CompareWithASCIIString(key, "sequence") == 0 && !sequence_obj) { sequence_obj = value; }
7494 else if (PyUnicode_CompareWithASCIIString(key, "view") == 0 && !view_obj) { view_obj = value; }
7495 else {
7496 PyErr_Format(PyExc_TypeError, "Got an unexpected keyword argument '%U'", key);
7497 return -1;
7498 }
7499 }
7500 }
7501
7502 // Parse view flag
7503 if (view_obj) {
7504 view = PyObject_IsTrue(view_obj);
7505 if (view == -1) return -1;
7506 }
7507
7508 // If no sequence provided, create empty Strs
7509 if (!sequence_obj) {
7510 self->layout = STRS_FRAGMENTED;
7511 self->data.fragmented.count = 0;
7512 self->data.fragmented.spans = NULL;
7513 sz_memory_allocator_init_default(&self->data.fragmented.allocator);
7514 self->data.fragmented.parent = NULL;
7515 return 0;
7516 }
7517
7518 // Check if it's an Arrow array (has `__arrow_c_array__` method)
7519 PyObject *arrow_method = PyObject_GetAttrString(sequence_obj, "__arrow_c_array__");
7520 if (arrow_method) {
7521 Py_DECREF(arrow_method);
7522 return Strs_init_from_pyarrow(self, sequence_obj, view);
7523 }
7524
7525 // Handle more traditional Python sequences
7526 PyErr_Clear(); // Clear the attribute error from checking for `__arrow_c_array__`
7527
7528 if (PyTuple_Check(sequence_obj)) { return Strs_init_from_tuple(self, sequence_obj, view); }
7529 else if (PyList_Check(sequence_obj)) { return Strs_init_from_list(self, sequence_obj, view); }
7530 else if (PyObject_HasAttrString(sequence_obj, "__iter__")) {
7531 return Strs_init_from_iterable(self, sequence_obj, view);

Callers

nothing calls this directly

Calls 5

Strs_init_from_pyarrowFunction · 0.85
Strs_init_from_tupleFunction · 0.85
Strs_init_from_listFunction · 0.85
Strs_init_from_iterableFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…