-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathstringwriter_extra_ops.h
More file actions
77 lines (63 loc) · 2.1 KB
/
stringwriter_extra_ops.h
File metadata and controls
77 lines (63 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#ifndef STRINGWRITER_EXTRA_OPS_H
#define STRINGWRITER_EXTRA_OPS_H
#ifdef MYPYC_EXPERIMENTAL
#include <stdint.h>
#include <Python.h>
#include "strings/librt_strings.h"
static inline CPyTagged
CPyStringWriter_Len(PyObject *obj) {
return (CPyTagged)((StringWriterObject *)obj)->len << 1;
}
static inline bool
CPyStringWriter_EnsureSize(StringWriterObject *data, Py_ssize_t n) {
if (likely(data->capacity - data->len >= n)) {
return true;
} else {
return LibRTStrings_grow_string_buffer(data, n);
}
}
static inline char
CPyStringWriter_Append(PyObject *obj, int32_t value) {
StringWriterObject *self = (StringWriterObject *)obj;
char kind = self->kind;
// Fast path: kind 1 (ASCII/Latin-1) with character < 256
if (kind == 1 && (uint32_t)value < 256) {
// Store length in local variable to enable additional optimizations
Py_ssize_t len = self->len;
if (!CPyStringWriter_EnsureSize(self, 1))
return CPY_NONE_ERROR;
self->buf[len] = (char)value;
self->len = len + 1;
return CPY_NONE;
}
// Slow path: handles kind switching and other cases
return LibRTStrings_string_append_slow_path(self, value);
}
// If index is negative, convert to non-negative index (no range checking)
static inline int64_t CPyStringWriter_AdjustIndex(PyObject *obj, int64_t index) {
if (index < 0) {
return index + ((StringWriterObject *)obj)->len;
}
return index;
}
static inline bool CPyStringWriter_RangeCheck(PyObject *obj, int64_t index) {
return index >= 0 && index < ((StringWriterObject *)obj)->len;
}
static inline int32_t CPyStringWriter_GetItem(PyObject *obj, int64_t index) {
StringWriterObject *self = (StringWriterObject *)obj;
char kind = self->kind;
char *buf = self->buf;
if (kind == 1) {
return (uint8_t)buf[index];
} else if (kind == 2) {
uint16_t val;
memcpy(&val, buf + index * 2, 2);
return (int32_t)val;
} else {
uint32_t val;
memcpy(&val, buf + index * 4, 4);
return (int32_t)val;
}
}
#endif // MYPYC_EXPERIMENTAL
#endif