Copying Bio-python to globplot to satisfy the dependency
[jabaws.git] / binaries / src / globplot / biopython-1.50 / Bio / stringfns.py
1 # Copyright 2000 by Jeffrey Chang.  All rights reserved.
2 # This code is part of the Biopython distribution and governed by its
3 # license.  Please see the LICENSE file that should have been included
4 # as part of this package.
5
6 """This provides useful general functions for working with strings (DEPRECATED).
7
8 This module and its C code equivalent are considered to be deprecated, and
9 are likely to be removed in a future release of Biopython.  Please get in
10 touch via the mailing list if this will affect you.
11
12 Functions:
13 splitany       Split a string using many delimiters.
14 find_anychar   Find one of a list of characters in a string.
15 rfind_anychar  Find one of a list of characters in a string, from end to start.
16
17 """
18 import warnings
19 warnings.warn("Bio.stringfns and its C code equivalent Bio.cstringfns are" \
20               +" deprecated, and will be removed in a future release of"\
21               +" Biopython.  If you want to continue to use this code,"\
22               +" please get in contact with the Biopython developers via"\
23               +" the mailing lists to avoid its permanent removal from"\
24               +" Biopython.", \
25               DeprecationWarning)
26
27 def splitany(s, sep=" \011\012\013\014\015", maxsplit=None, negate=0):
28     """splitany(s [,sep [,maxsplit [,negate]]]) -> list of strings
29
30     Split a string.  Similar to string.split, except that this considers
31     any one of the characters in sep to be a delimiter.  If negate is
32     true, then everything but sep will be a separator.
33
34     """
35     strlist = []
36     prev = 0
37     for i in range(len(s)):
38         if maxsplit is not None and len(strlist) >= maxsplit:
39             break
40         if (s[i] in sep) == (not negate):
41             strlist.append(s[prev:i])
42             prev = i+1
43     strlist.append(s[prev:])
44     return strlist
45
46 def find_anychar(string, chars, index=None, negate=0):
47     """find_anychar(string, chars[, index]) -> index of a character or -1
48
49     Find a character in string.  chars is a list of characters to look
50     for.  Return the index of the first occurrence of any of the
51     characters, or -1 if not found.  index is the index where the
52     search should start.  By default, I search from the beginning of
53     the string.
54     
55     """
56     if index is None:
57         index = 0
58     while index < len(string) and \
59           ((not negate and string[index] not in chars) or
60            (negate and string[index] in chars)):
61         index += 1
62     if index == len(string):
63         return -1
64     return index
65
66 def rfind_anychar(string, chars, index=None, negate=0):
67     """rfind_anychar(string, chars[, index]) -> index of a character or -1
68
69     Find a character in string, looking from the end to the start.
70     chars is a list of characters to look for.  Return the index of
71     the first occurrence of any of the characters, or -1 if not found.
72     index is the index where the search should start.  By default, I
73     search from the end of the string.
74     
75     """
76     if index is None:
77         index = len(string)-1
78     while index >= 0 and \
79           ((not negate and string[index] not in chars) or
80            (negate and string[index] in chars)):
81         index -= 1
82     # If not found, index will already be -1.
83     return index
84
85 # Try and load C implementations of functions.  If I can't,
86 # then just ignore and use the pure python implementations.
87 try:
88     from cstringfns import *
89 except ImportError:
90     pass