fix prev_word

This commit is contained in:
chrys87 2016-07-21 15:41:02 +02:00 committed by GitHub
parent 5c9327a575
commit 44b4c59d28

View File

@ -4,9 +4,24 @@ def getPrevWord(currX,currY, currText):
if currText == '': if currText == '':
return -1, -1, '' return -1, -1, ''
x, y, word = getCurrentWord(currX,currY,currText) x, y, word = getCurrentWord(currX,currY,currText)
if (x == currX) or (y == currY) and (word == ''): wrappedLines = currText.split('\n')
if (word == ''):
return currX, currY, '' return currX, currY, ''
return getCurrentWord(x - 2, y, currText) while True:
if x < 2:
if y != 0:
y -= 1
else:
return currX, currY, ''
x = len(wrappedLines[y]) - 1
else:
x -= 1
if wrappedLines[y] != '':
break
x, y, word = getCurrentWord(x, y, currText)
if word == '':
return currX, currY, ''
return x, y, word
def getCurrentWord(currX,currY, currText): def getCurrentWord(currX,currY, currText):
if currText == '': if currText == '':
@ -16,40 +31,38 @@ def getCurrentWord(currX,currY, currText):
wrappedLines = currText.split('\n') wrappedLines = currText.split('\n')
wordFound = False wordFound = False
currWord = '' currWord = ''
if x < 0:
if y != 0:
y -= 1
currLine = wrappedLines[y].replace("\t"," ")
else:
return currX, currY, ''
x = len(currLine) - 1
else:
currLine = wrappedLines[y].replace("\t"," ") currLine = wrappedLines[y].replace("\t"," ")
if currLine[x] == ' ' and x > 1:
x = x - 2
while not wordFound: while not wordFound:
x = currLine[:x].rfind(" ") x = currLine[:x].rfind(" ")
if x == -1: if x == -1:
x = 0 x = 0
else: else:
x += 1 x += 1
wordEnd = currLine[x + 2:].find(" ") wordEnd = currLine[x + 1:].find(" ")
if wordEnd == -1: if wordEnd == -1:
wordEnd = len(currLine[x:]) wordEnd = len(currLine)
else: else:
wordEnd += x + 1 wordEnd += x + 1
currWord = currLine[x:wordEnd] currWord = currLine[x:wordEnd]
wordFound = currWord.strip(" \t\n") != '' wordFound = currWord.strip(" \t\n") != ''
print(currWord)
if wordFound: if wordFound:
break break
print(currWord)
if x == 0: if x == 0:
if y != 0: if y != 0:
y -= 1 y -= 1
currLine = wrappedLines[y].replace("\t"," ") currLine = wrappedLines[y].replace("\t"," ")
else: else:
return currX, currY, '' return currX, currY, ''
x = len(currLine) - 1 x = len(wrappedLines[y]) - 1
else: else:
x -= 1 x -= 1
print(currWord)
return x, y, currWord return x, y, currWord
currText = " das ist ein test\ntest das\ntesttest\n\ntest"
currY = 4
currX = 3
currX, currY, word = getCurrentWord(currX,currY,currText)
#currX, currY, word = getPrevWord(currX,currY,currText)
print(currX, currY, word)