~ubuntu-branches/ubuntu/trusty/pycalendar/trusty

« back to all changes in this revision

Viewing changes to src/pycalendar/stringutils.py

  • Committer: Package Import Robot
  • Author(s): Rahul Amaram
  • Date: 2012-05-29 12:42:34 UTC
  • Revision ID: package-import@ubuntu.com-20120529124234-mzwu4vw4chh0sc1f
Tags: upstream-2.0~svn188
ImportĀ upstreamĀ versionĀ 2.0~svn188

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##
 
2
#    Copyright (c) 2007-2011 Cyrus Daboo. All rights reserved.
 
3
#    
 
4
#    Licensed under the Apache License, Version 2.0 (the "License");
 
5
#    you may not use this file except in compliance with the License.
 
6
#    You may obtain a copy of the License at
 
7
#    
 
8
#        http://www.apache.org/licenses/LICENSE-2.0
 
9
#    
 
10
#    Unless required by applicable law or agreed to in writing, software
 
11
#    distributed under the License is distributed on an "AS IS" BASIS,
 
12
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
#    See the License for the specific language governing permissions and
 
14
#    limitations under the License.
 
15
##
 
16
 
 
17
from hashlib import md5
 
18
 
 
19
def strduptokenstr(txt, tokens):
 
20
    
 
21
    result = None
 
22
    start = 0
 
23
 
 
24
    # First punt over any leading space
 
25
    for s in txt:
 
26
        if s == " ":
 
27
            start += 1
 
28
        else:
 
29
            break
 
30
    else:
 
31
        return None, ""
 
32
 
 
33
    # Handle quoted string
 
34
    if txt[start] == '\"':
 
35
        
 
36
        maxlen = len(txt)
 
37
        # Punt leading quote
 
38
        start += 1
 
39
        end = start
 
40
 
 
41
        done = False
 
42
        while not done:
 
43
            if end == maxlen:
 
44
                return None, txt
 
45
 
 
46
            if txt[end] == '\"':
 
47
                done = True
 
48
            elif txt[end] == '\\':
 
49
                # Punt past quote
 
50
                end += 2
 
51
            else:
 
52
                end += 1
 
53
            if end >= maxlen:
 
54
                return None, txt
 
55
 
 
56
        return txt[start:end], txt[end + 1:]
 
57
    else:
 
58
        for relend, s in enumerate(txt[start:]):
 
59
            if s in tokens:
 
60
                if relend:
 
61
                    result = txt[start:start+relend]
 
62
                else:
 
63
                    result = ""
 
64
                return result, txt[start+relend:]
 
65
        return txt[start:], ""
 
66
 
 
67
def strtoul(s, offset=0):
 
68
    
 
69
    max = len(s)
 
70
    startoffset = offset
 
71
    while offset < max:
 
72
        if s[offset] in "0123456789":
 
73
            offset += 1
 
74
            continue
 
75
        elif offset == 0:
 
76
            raise ValueError
 
77
        else:
 
78
            return int(s[startoffset:offset]), offset
 
79
    else:
 
80
        if offset == 0:
 
81
            raise ValueError
 
82
        else:
 
83
            return int(s[startoffset:]), offset
 
84
 
 
85
def strindexfind(s, ss, default_index):
 
86
    if s and ss:
 
87
        i = 0
 
88
        s = s.upper()
 
89
        while ss[i]:
 
90
            if s == ss[i]:
 
91
                return i
 
92
            i += 1
 
93
 
 
94
    return default_index
 
95
 
 
96
def strnindexfind(s, ss, default_index):
 
97
    if s and ss:
 
98
        i = 0
 
99
        s = s.upper()
 
100
        while ss[i]:
 
101
            if s.startswith(ss[i]):
 
102
                return i
 
103
            i += 1
 
104
 
 
105
    return default_index
 
106
 
 
107
def compareStringsSafe(s1, s2):
 
108
    if s1 is None and s2 is None:
 
109
        return True
 
110
    elif (s1 is None and s2 is not None) or (s1 is not None and s2 is None):
 
111
        return False
 
112
    else:
 
113
        return s1 == s2
 
114
 
 
115
def md5digest(txt):
 
116
    return md5.new(txt).hexdigest()