class Utils: # delimiter list delimNode = " .;]=\n\r,}" # see if a character is present in a string def checkCharacter(self, char, string): i = len(string) i1 = 0 done = "F" while i1 < i and done == "F": if char == string[i1]: done = "T" i1 = i1 + 1 return done # takec a b will parse character a from string b # and returns (True, b-a) or (False, b) def takec(self,char, string): if len(string) == 0: return ("F", "") else: if char == string[0]: return ("T", string[1:]) else: return ("F", string) # parseUntil( a, b) will take from string b until char a is met # and returns (True, c, b-c) where c is the part of b before a with # a not included or it returns (False, "", b) def parseUntil(self, char, inString): if len(inString) == 0: return ("F", "", "") else: list = self.parseUntilHelp ("F", char, "", inString) # if len(list[3]) == 0: # print "parseUntil ",char, ord(char), inString if list[0] == "F": return ("F", list[2], list[3]) else: return ("T", list[2], list[3]) # help function for parseUntil def parseUntilHelp(self, bool, char, string1, string2): while len(string2) > 0: if string2[0] == char: return ("T", char, string1, string2[1:]) else: if len(string2) > 1: string1 = string1 + string2[0] string2 = string2[1:] else: return ("F", char, string1, "") return ("F", char, string1, "") # parseUntilString (s1, s2) will parse string s1 until string s2 is met. # True or False is returned; the parsed string and the rest string with # s2 still included def parseUntilString(self, s1, s2): if (len(s1) == 0 or len(s2) == 0): return ("F", s1, s2) bool, first, rest = self.parseUntil(s2[0:1],s1) if (bool == "T" and self.startsWith(rest, s2[1:]) == "T"): return ("T", first, s2[0:1] + rest) elif bool == "F": return ("F", s1, s2) else: bool1, first1, rest1 = self.parseUntilString(rest, s2) return (bool1, first + s2[0:1] + first1, rest1) # parseUntilDelim delim input will take a list with delimiters and parse the # input string till one of the delimiters is found . # It returns (True c input-c) where c is the parsed string without the # delimiter or it returns (False "" input) if no result def parseUntilDelim(self, delimString, s): if len(s )== 0: return ("F", "", "") else: return self.parseUntilDelimHelp(delimString, "", s) # This is a help function . # must be called with s = "" def parseUntilDelimHelp(self, delimString, s, input): if len(input) == 0: return ("F", s, "") else: if (self.elem (input[0], delimString)) == "T": return ("T", s, input) else: return self.parseUntilDelimHelp (delimString, s + input[0], input[1:]) # see if a character occurs (at least one time) in a string. def elem(self, char, string): if len(string) == 0: return "F" else: if (char == string[0]): return "T" else: return self.elem (char, string[1:]) # skipTillCharacter skip the input string till the character is met . # The rest of the string is returned without the character. def skipTillCharacter(self, c, input): if len(input) == 0: return "" else: if input[0] == c: return input[1:] else: return self.skipTillCharacter(c, input[1:]) # test if the following character is a blanc. def testBlancs(self, char): if char == " " or char == "\n" or char == "\r" or char == "\t": return "T" else: return "F" # skips the blancs in a string and returns the stripped string. def skipBlancs(self, s): if len(s) == 0: return "" else: while not len(s) == 0: x = s[0] if x == "#": s = self.parseComment(s) elif x == " " or x == "\n" or x == "\r" or x == "\t": s = s[1:] else: return s return "" # skips the blancs but no comment in a string and returns # the stripped string. def skipBlancs1(self, s): if len(s) == 0: return "" else: while not len(s) == 0: x = s[0] if x == " " or x == "\n" or x == "\r": s = s[1:] else: return s return "" # function for parsing comment. # input is the string to be parsed. # the comment goes till "\r\n" is encountered. # returns a string with the comment striped off. def parseComment(self, s): if len(s) == 0: return "" else: l1 = self.takec( "#", s) # format (Bool,String) l2 = self.parseUntil ("\n", l1[1]) # format (Bool, String, String) if l1[0] == "T" and l2[0] == "T": return self.skipBlancs( l2[2]) else: return "Error parsing comment" + s # startsWith looks if a string starts with a certain chain. # the second parameter is the starting string. def startsWith(self, s1, s2): if len(s1) == 0 and len(s2) == 0: return "T" elif len(s1) == 0: return "F" elif len(s2) == 0: return "T" elif s1[0] == s2[0]: return self.startsWith(s1[1:], s2[1:]) else: return "F" # containsString test whether the first string is contained in the # second string. def containsString(self, s1, s2): if len(s1) == 0: return "T" elif len(s2) == 0: return "F" elif s1[0] == s2[0]: return self.containsString(s1[1:], s2[1:]) else: return self.containsString (s1, s2[1:]) # parseString a b will parse string a from string b # and returns (True, b-a) or (False,b). def parseString (self, a, b): a1 = len (a) s = b[0:a1] c = b[a1:] if s == a: return ("T", c) else: return ("F", b) # tests of this class utils = Utils() string = "bcdefgajefak\ni;uyt" #print ("Test string: " + string) #print ("Test parseUntil: ", utils.parseUntil ("\n", string)) #print ("Test takec: ", utils.takec ("b", string)) #print ("Test takec: ", utils.takec ("d", string)) #print ("Test checkCharacter: ", utils.checkCharacter ("j", string)) #print ("Test elem: " + utils.elem("g", string)) #print ("Test elem: " + utils.elem("x", string)) #print ("delimNode: " + utils.delimNode) #l1 = utils.parseUntilDelim(utils.delimNode, string) #print ("Test parseUntilDelim: " ,l1) #print ("Test skipTillCharacter: " + utils.skipTillCharacter("u", string)) #print ("Test testBlancs: " + utils.testBlancs("\n") + utils.testBlancs("r")) #print ("Test skipBlancs: " + utils.skipBlancs("\n ggg")) #print ("Test parseComment: " + utils.parseComment("# hjshhd \r\n dskjkjk")) #print ("Test startsWith: " , utils.startsWith(string, "bcdef")) #print ("Test startsWith: " , utils.startsWith(string, "dbcdef")) #print ("Test containsString: " , utils.containsString("gajk", string)) #print ("Test containsString: " , utils.containsString("$$gajk", string)) #print ("Test parseString: " , utils.parseString("bcd", string)) #print ("Test parseString: " , utils.parseString("sbcd", string)) #print (utils.delimNode) #print ("Test parseUntilString: ", utils.parseUntilString(string, "efa"))