22
22
from bzrlib.dirstate import DirState
25
28
cdef extern from "Python.h":
26
29
# GetItem returns a borrowed reference
27
30
void *PyDict_GetItem(object p, object key)
29
32
object PyList_GetItem(object lst, int index)
30
33
object PyTuple_GetItem(object tpl, int index)
35
char* PyString_AsString(object p)
36
int PyString_Size(object p)
39
cdef extern from "string.h":
40
int strncmp(char *s1, char *s2, size_t len)
41
int strcmp(char *s1, char *s2)
42
char *strchr(char *s1, char c)
33
45
cdef object _split_from_path(object cache, object path):
34
46
"""get the dirblock tuple for a given path.
85
cdef int _cmp_dirblock_strings(char *path1, int size1, char *path2, int size2):
86
"""This compares 2 strings separating on path sections.
88
This is equivalent to "cmp(path1.split('/'), path2.split('/'))"
89
However, we don't want to create an extra object for doing the split.
91
:param path1: The first path to compare
92
:param size1: The length of the first path
93
:param path2: The second path
94
:param size1: The length of the second path
95
:return: 0 if they are equal, -1 if path1 comes first, 1 if path2 comes
114
# Ensure that we are pointing to the final NULL terminator on both ends
115
assert end1[0] == c'\x00'
116
assert end2[0] == c'\x00'
118
while base1 < end1 and base2 < end2:
119
# Find the next path separator
120
# (This is where you would like strchrnul)
121
tip1 = strchr(base1, c'/')
122
tip2 = strchr(base2, c'/')
129
cur_len1 = tip1 - base1
130
cur_len2 = tip2 - base2
132
if cur_len2 < cur_len1:
135
diff = strncmp(base1, base2, cmp_len)
136
# print 'comparing "%s", "%s", %d = %d' % (base1, base2, cmp_len, diff)
139
if cur_len1 < cur_len2:
141
elif cur_len1 > cur_len2:
145
# Do we still have uncompared characters?
153
def cmp_dirblock_strings(path1, path2):
154
"""Compare to python strings in dirblock fashion."""
155
return _cmp_dirblock_strings(PyString_AsString(path1),
156
PyString_Size(path1),
157
PyString_AsString(path2),
158
PyString_Size(path2))
73
161
def bisect_dirblock(dirblocks, dirname, lo=0, hi=None, cache=None):
74
162
"""Return the index where to insert dirname into the dirblocks.