| 3275 | " TypeError: If the table is not a string or dictionary."; |
| 3276 | |
| 3277 | static PyObject *Str_like_translate(PyObject *self, PyObject *const *args, Py_ssize_t positional_args_count, |
| 3278 | PyObject *args_names_tuple) { |
| 3279 | int is_member = self != NULL && PyObject_TypeCheck(self, &StrType); |
| 3280 | if (positional_args_count < !is_member + 1 || positional_args_count > !is_member + 4) { |
| 3281 | PyErr_Format(PyExc_TypeError, "Invalid number of arguments"); |
| 3282 | return NULL; |
| 3283 | } |
| 3284 | |
| 3285 | PyObject *str_obj = is_member ? self : args[0]; |
| 3286 | PyObject *look_up_table_obj = args[!is_member]; |
| 3287 | PyObject *inplace_obj = positional_args_count > !is_member + 1 ? args[!is_member + 1] : NULL; |
| 3288 | PyObject *start_obj = positional_args_count > !is_member + 2 ? args[!is_member + 2] : NULL; |
| 3289 | PyObject *end_obj = positional_args_count > !is_member + 3 ? args[!is_member + 3] : NULL; |
| 3290 | |
| 3291 | // Optional keyword arguments |
| 3292 | if (args_names_tuple) { |
| 3293 | Py_ssize_t args_names_count = PyTuple_GET_SIZE(args_names_tuple); |
| 3294 | for (Py_ssize_t i = 0; i < args_names_count; ++i) { |
| 3295 | PyObject *key = PyTuple_GET_ITEM(args_names_tuple, i); |
| 3296 | PyObject *value = args[positional_args_count + i]; |
| 3297 | if (PyUnicode_CompareWithASCIIString(key, "inplace") == 0 && !inplace_obj) { inplace_obj = value; } |
| 3298 | else if (PyUnicode_CompareWithASCIIString(key, "start") == 0 && !start_obj) { start_obj = value; } |
| 3299 | else if (PyUnicode_CompareWithASCIIString(key, "end") == 0 && !end_obj) { end_obj = value; } |
| 3300 | else if (PyErr_Format(PyExc_TypeError, "Got an unexpected keyword argument '%U'", key)) |
| 3301 | return NULL; |
| 3302 | } |
| 3303 | } |
| 3304 | |
| 3305 | // Optional start and end arguments |
| 3306 | Py_ssize_t start = 0, end = PY_SSIZE_T_MAX; |
| 3307 | |
| 3308 | if (start_obj && ((start = PyLong_AsSsize_t(start_obj)) == -1 && PyErr_Occurred())) { |
| 3309 | PyErr_SetString(PyExc_TypeError, "start must be an integer"); |
| 3310 | return NULL; |
| 3311 | } |
| 3312 | |
| 3313 | if (end_obj && ((end = PyLong_AsSsize_t(end_obj)) == -1 && PyErr_Occurred())) { |
| 3314 | PyErr_SetString(PyExc_TypeError, "end must be an integer"); |
| 3315 | return NULL; |
| 3316 | } |
| 3317 | |
| 3318 | sz_string_view_t str; |
| 3319 | if (!sz_py_export_string_like(str_obj, &str.start, &str.length)) { |
| 3320 | wrap_current_exception("First argument must be string-like"); |
| 3321 | return NULL; |
| 3322 | } |
| 3323 | |
| 3324 | sz_string_view_t look_up_table_str; |
| 3325 | sz_align_(64) char look_up_table[256]; |
| 3326 | if (PyDict_Check(look_up_table_obj)) { |
| 3327 | |
| 3328 | // If any character is not defined, it will be replaced with itself: |
| 3329 | for (int i = 0; i < 256; i++) look_up_table[i] = (char)i; |
| 3330 | |
| 3331 | // Process the dictionary into the look-up table |
| 3332 | PyObject *key, *value; |
| 3333 | Py_ssize_t pos = 0; |
| 3334 | while (PyDict_Next(look_up_table_obj, &pos, &key, &value)) { |
nothing calls this directly
no test coverage detected
searching dependent graphs…