1
|
|
|
# |
2
|
|
|
# Copyright 2001 - 2016 Ludek Smid [http://www.ospace.net/] |
3
|
|
|
# |
4
|
|
|
# This file is part of Pygame.UI. |
5
|
|
|
# |
6
|
|
|
# Pygame.UI is free software; you can redistribute it and/or modify |
7
|
|
|
# it under the terms of the Lesser GNU General Public License as published by |
8
|
|
|
# the Free Software Foundation; either version 2.1 of the License, or |
9
|
|
|
# (at your option) any later version. |
10
|
|
|
# |
11
|
|
|
# Pygame.UI is distributed in the hope that it will be useful, |
12
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
# Lesser GNU General Public License for more details. |
15
|
|
|
# |
16
|
|
|
# You should have received a copy of the Lesser GNU General Public License |
17
|
|
|
# along with Pygame.UI; if not, write to the Free Software |
18
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
19
|
|
|
# |
20
|
|
|
|
21
|
|
|
delimiters = [" ", ".", "-", "(", ")", "{", "}", "[", "]", ",", ":",\ |
22
|
|
|
";", "/", "\\", "\"", "<", ">", "?", "!", "+", "*", "&",\ |
23
|
|
|
"^", "@", "#", "$", "%", "^", "=", "|", "~"] |
24
|
|
|
|
25
|
|
|
def splitter(text, delims = None): |
26
|
|
|
"""Splits given string into parts separated by more delimiters. |
27
|
|
|
|
28
|
|
|
delims parameter is list of delimiter characters. Function returns |
29
|
|
|
tuple containg word, starting postion of word and ending position |
30
|
|
|
of word. Given word can be taken from text as text[start:end]. |
31
|
|
|
Example: |
32
|
|
|
text = "This is line" |
33
|
|
|
result = "" |
34
|
|
|
for word in splitter(text, [" "]): |
35
|
|
|
result = result + text[word[1]:word[2]] + " " |
36
|
|
|
print result |
37
|
|
|
result = "" |
38
|
|
|
for word in splitter(text, [" "]): |
39
|
|
|
result = result + word[0] + " " |
40
|
|
|
print result |
41
|
|
|
""" |
42
|
|
|
#stores result |
43
|
|
|
result = [] |
44
|
|
|
#if delimiter is not specified, use defaults |
45
|
|
|
if delims == None: |
46
|
|
|
delims = delimiters |
47
|
|
|
#starting with empty word |
48
|
|
|
word = "" |
49
|
|
|
#starting position of word |
50
|
|
|
start = 0 |
51
|
|
|
#traverse string by chars |
52
|
|
|
for i in range(len(text)): |
53
|
|
|
#test, if char is delimiter |
54
|
|
|
if text[i] in delims: |
55
|
|
|
#is delimiter, so add new word (non empty) to result |
56
|
|
|
if len(word) > 0: |
57
|
|
|
result.append((word, start, i)) |
58
|
|
|
#start new word |
59
|
|
|
word = "" |
60
|
|
|
#skip delimiter |
61
|
|
|
start = i + 1 |
62
|
|
|
else: |
63
|
|
|
#char is not delimiter, so append it to word |
64
|
|
|
word = word + text[i] |
65
|
|
|
|
66
|
|
|
#append last word to result |
67
|
|
|
if len(word) > 0: |
68
|
|
|
#handle end of text with no delimiter |
69
|
|
|
if start + len(word) == len(text): |
70
|
|
|
#word end beyond end of text |
71
|
|
|
i = i + 1 |
|
|
|
|
72
|
|
|
result.append((word, start, i)) |
73
|
|
|
|
74
|
|
|
return result |
75
|
|
|
|