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

Function Strs_getitem

python/stringzilla.c:2001–2031  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1999}
2000
2001static PyObject *Strs_getitem(Strs *self, Py_ssize_t i) {
2002
2003 // Check for negative index and convert to positive
2004 Py_ssize_t count = Strs_len(self);
2005 if (i < 0) i += count;
2006 if (i < 0 || i >= count) {
2007 PyErr_SetString(PyExc_IndexError, "Index out of range");
2008 return NULL;
2009 }
2010
2011 get_string_at_offset_t getter = str_at_offset_getter(self);
2012 if (!getter) {
2013 PyErr_SetString(PyExc_TypeError, "Unknown Strs kind");
2014 return NULL;
2015 }
2016
2017 PyObject *memory_owner = NULL;
2018 sz_cptr_t start = NULL;
2019 sz_size_t length = 0;
2020 getter(self, i, count, &memory_owner, &start, &length);
2021
2022 // Create a new `Str` object
2023 Str *view_copy = (Str *)StrType.tp_alloc(&StrType, 0);
2024 if (view_copy == NULL && PyErr_NoMemory()) return NULL;
2025
2026 view_copy->memory.start = start;
2027 view_copy->memory.length = length;
2028 view_copy->parent = memory_owner;
2029 Py_XINCREF(memory_owner);
2030 return view_copy;
2031}
2032
2033/**
2034 * This returns a `Strs` object of a potentially different layout:

Callers 1

Strs_subscriptFunction · 0.85

Calls 2

Strs_lenFunction · 0.85
str_at_offset_getterFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…