Skip to content

Commit 781de00

Browse files
committed
use kstring_t instead of manual string allocation
1 parent 2f98364 commit 781de00

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

pysam/libcalignedsegment.pyx

+13-16
Original file line numberDiff line numberDiff line change
@@ -1321,8 +1321,9 @@ cdef class AlignedSegment:
13211321
cdef int ret
13221322
cdef int pos = 0
13231323
cdef int size = 16
1324-
cdef char buf[16]
1325-
cdef char *s = buf
1324+
cdef kstring_t buf
1325+
buf.l = buf.m = 0
1326+
buf.s = NULL
13261327

13271328
src = self._delegate
13281329
if pysam_get_n_cigar(src) == 0:
@@ -1333,22 +1334,18 @@ cdef class AlignedSegment:
13331334
for k from 0 <= k < pysam_get_n_cigar(src):
13341335
op = cigar_p[k] & BAM_CIGAR_MASK
13351336
l = cigar_p[k] >> BAM_CIGAR_SHIFT
1336-
ret = snprintf(s + pos, size - pos, "%u%c", l, CODE2CIGAR[op])
1337-
if ret >= size - pos:
1338-
pos = 0
1339-
size = size * 2
1340-
if s != buf:
1341-
free(s)
1342-
s = <char *>malloc(size)
1343-
break
1344-
pos += ret
1337+
kputl(l, &buf)
1338+
kputc(<int>(CODE2CIGAR[op]), &buf)
13451339
else:
13461340
break
1347-
try:
1348-
return s[:pos].decode("utf8")
1349-
finally:
1350-
if s != buf:
1351-
free(s)
1341+
1342+
ret = force_str(buf.s[:buf.l])
1343+
1344+
if buf.m:
1345+
free(buf.s)
1346+
1347+
return ret
1348+
13521349
def __set__(self, cigar):
13531350
if cigar is None or len(cigar) == 0:
13541351
self.cigartuples = []

0 commit comments

Comments
 (0)