-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharIndexer.py
More file actions
46 lines (37 loc) · 1.31 KB
/
Copy pathCharIndexer.py
File metadata and controls
46 lines (37 loc) · 1.31 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
class CharIndexer:
"""
find the character index within a file
"""
def __init__(self,doc):
self.lines=[] # (charStart,charEnd)
charEnd=-1
for line in doc.split('\n'):
charStart=charEnd+1
charEnd=charStart+len(line)
self.lines.append((charStart,charEnd))
def charIndexToLineChar(self,charIndex,zeroBasedIndexing=False):
"""
convert an absolute character index to a line number + character offset
if zeroBasedIndexing=True, then the first line,character is 0,0
otherwise, it is 1,1
returns (line,char)
If the line is not in the given file, return None
"""
for lineNo in range(len(self.lines)):
if charIndex<=self.lines[lineNo][1]:
if zeroBasedIndexing:
return (lineNo,charIndex-self.lines[lineNo][0])
else:
return (lineNo+1,charIndex-self.lines[lineNo][0]+1)
return None
def charIndexToFilenameIndicator(self,charIndex,zeroBasedIndexing=False,charOffsetSep=',',includeCharOffset=True):
"""
if zeroBasedIndexing=True, then the first line,character is 0,0
otherwise, it is 1,1
return the charIndex to an :line,char indicator to append to a filename,
such as foo.txt:21,4
"""
idx=self.charIndexToLineChar(charIndex,zeroBasedIndexing)
if includeCharOffset:
return ':'+str(idx[0])+charOffsetSep+str(str(idx[1]))
return ':'+str(idx[0])