785a260a1308800831cf106f049336e449753d45
[scons.git] / src / engine / SCons / compat / _scons_UserString.py
1 #
2 # __COPYRIGHT__
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and associated documentation files (the
6 # "Software"), to deal in the Software without restriction, including
7 # without limitation the rights to use, copy, modify, merge, publish,
8 # distribute, sublicense, and/or sell copies of the Software, and to
9 # permit persons to whom the Software is furnished to do so, subject to
10 # the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included
13 # in all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
16 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
17 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #
23
24 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
25
26 __doc__ = """
27 A user-defined wrapper around string objects
28
29 This class is "borrowed" from the Python 2.2 UserString and modified
30 slightly for use with SCons.  It is *NOT* guaranteed to be fully compliant
31 with the standard UserString class from all later versions of Python.
32 In particular, it does not necessarily contain all of the methods found
33 in later versions.
34 """
35
36 import types
37
38 StringType = types.StringType
39
40 if hasattr(types, 'UnicodeType'):
41     UnicodeType = types.UnicodeType
42     def is_String(obj):
43         return type(obj) in (StringType, UnicodeType)
44 else:
45     def is_String(obj):
46         return type(obj) is StringType
47
48 class UserString:
49     def __init__(self, seq):
50         if is_String(seq):
51             self.data = seq
52         elif isinstance(seq, UserString):
53             self.data = seq.data[:]
54         else:
55             self.data = str(seq)
56     def __str__(self): return str(self.data)
57     def __repr__(self): return repr(self.data)
58     def __int__(self): return int(self.data)
59     def __long__(self): return long(self.data)
60     def __float__(self): return float(self.data)
61     def __complex__(self): return complex(self.data)
62     def __hash__(self): return hash(self.data)
63
64     def __cmp__(self, string):
65         if isinstance(string, UserString):
66             return cmp(self.data, string.data)
67         else:
68             return cmp(self.data, string)
69     def __contains__(self, char):
70         return char in self.data
71
72     def __len__(self): return len(self.data)
73     def __getitem__(self, index): return self.__class__(self.data[index])
74     def __getslice__(self, start, end):
75         start = max(start, 0); end = max(end, 0)
76         return self.__class__(self.data[start:end])
77
78     def __add__(self, other):
79         if isinstance(other, UserString):
80             return self.__class__(self.data + other.data)
81         elif is_String(other):
82             return self.__class__(self.data + other)
83         else:
84             return self.__class__(self.data + str(other))
85     def __radd__(self, other):
86         if is_String(other):
87             return self.__class__(other + self.data)
88         else:
89             return self.__class__(str(other) + self.data)
90     def __mul__(self, n):
91         return self.__class__(self.data*n)
92     __rmul__ = __mul__
93
94 # Local Variables:
95 # tab-width:4
96 # indent-tabs-mode:nil
97 # End:
98 # vim: set expandtab tabstop=4 shiftwidth=4: