~pbms-core/pbms/5.11-beta

« back to all changes in this revision

Viewing changes to mybs/java/src/org/blobstreaming/lib/str.java

  • Committer: paul-mccullagh
  • Date: 2008-03-26 11:35:17 UTC
  • Revision ID: paul-mccullagh-afb1610c21464a577ae428d72fc725eb986c05a5
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2007 SNAP Innovation GmbH
 
2
 *
 
3
 * BLOB Streaming for MySQL
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
18
 *
 
19
 * Paul McCullagh
 
20
 *
 
21
 * 2007-07-30
 
22
 *
 
23
 * H&G2JCtL
 
24
 *
 
25
 * String utilities
 
26
 *
 
27
 */
 
28
 
 
29
package org.blobstreaming.lib;
 
30
 
 
31
public class str {
 
32
        /**
 
33
         * Index return is 0 for the first position in the string.
 
34
         * -1 means not found.
 
35
         */
 
36
        public static int locate(String from, String search, int count)
 
37
        {
 
38
                int i;
 
39
 
 
40
                if (count < 0) {
 
41
                        count = -count;
 
42
                        i = from.length();
 
43
                        while (count > 0) {
 
44
                                i = from.lastIndexOf(search, i-1);
 
45
                                if (i == -1)
 
46
                                        break;
 
47
                                count--;
 
48
                        }
 
49
                }
 
50
                else {
 
51
                        i = -1;
 
52
                        while (count > 0) {
 
53
                                i = from.indexOf(search, i+1);
 
54
                                if (i == -1)
 
55
                                        break;
 
56
                                count--;
 
57
                        }
 
58
                }
 
59
                return i;
 
60
        }
 
61
 
 
62
        public static int locate(String from, String search)
 
63
        {
 
64
                return locate(from, search, 1);
 
65
        }
 
66
 
 
67
        public static String left(String from, String search, int count)
 
68
        {
 
69
                int x = locate(from, search, count);
 
70
 
 
71
                if (x == -1) {
 
72
                        if (count >= 0)
 
73
                                return from;
 
74
                        return "";
 
75
                }
 
76
                return from.substring(0, x);
 
77
        }
 
78
 
 
79
        public static String left(String from, String search)
 
80
        {
 
81
                return left(from, search, 1);
 
82
        }
 
83
 
 
84
        public static String right(String from, String search, int count)
 
85
        {
 
86
                int x = locate(from, search, count);
 
87
 
 
88
                if (x == -1) {
 
89
                        if (count >= 0)
 
90
                                return "";
 
91
                        return from;
 
92
                }
 
93
                return from.substring(x+search.length());
 
94
        }
 
95
 
 
96
        public static String right(String from, String search)
 
97
        {
 
98
                return right(from, search, 1);
 
99
        }
 
100
        
 
101
        /**
 
102
         * Should trim CR and LF as well.
 
103
         */
 
104
        public static String trim(String s)
 
105
        {
 
106
                return s.trim();
 
107
        }
 
108
 
 
109
        public static String substr(String s, int offset, int len)
 
110
        {
 
111
                return s.substring(offset, offset+len);
 
112
        }
 
113
 
 
114
        public static boolean listContains(String list, String val)
 
115
        {
 
116
                String item;
 
117
 
 
118
                while (list != "") {
 
119
                        item = trim(left(list, ","));
 
120
                        if (item.equals(val))
 
121
                                return true;
 
122
                        list = trim(right(list, ","));
 
123
                }
 
124
                return false;
 
125
        }
 
126
 
 
127
 
 
128
        public static String replaceAll(String string, String replace, String with)
 
129
        {
 
130
                StringBuffer result;
 
131
 
 
132
                if (string == null)
 
133
                        return(null);
 
134
                result = new StringBuffer();
 
135
                while (locate(string, replace) >= 0) {
 
136
                        result.append(left(string, replace));
 
137
                        result.append(with);
 
138
                        string = right(string, replace);
 
139
                }
 
140
                result.append(string);
 
141
                return result.toString();
 
142
        }
 
143
 
 
144
        public static String base64encode(String value, int line_size)
 
145
        {
 
146
                return value;
 
147
        }
 
148
 
 
149
        public static String base64encode(String value)
 
150
        {
 
151
                return base64encode(value, 76);
 
152
        }
 
153
 
 
154
        public static String base64decode(String value)
 
155
        {
 
156
                return value;
 
157
        }
 
158
}