Typo: inspect_jpegs, not inspect-jpegs in form
[privoxy.git] / src / java / org / privoxy / activityconsole / StringUtil.java
1 /*********************************************************************
2  *
3  * File        :  $Source$
4  *
5  * Purpose     :  Utility string functions.
6  *
7  * Copyright   :  Written by and Copyright (C) 2003 the SourceForge
8  *                Privoxy team. http://www.privoxy.org/
9  *
10  *                Based on the Internet Junkbuster originally written
11  *                by and Copyright (C) 1997 Anonymous Coders and
12  *                Junkbusters Corporation.  http://www.junkbusters.com
13  *
14  *                This program is free software; you can redistribute it
15  *                and/or modify it under the terms of the GNU General
16  *                Public License as published by the Free Software
17  *                Foundation; either version 2 of the License, or (at
18  *                your option) any later version.
19  *
20  *                This program is distributed in the hope that it will
21  *                be useful, but WITHOUT ANY WARRANTY; without even the
22  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
23  *                PARTICULAR PURPOSE.  See the GNU General Public
24  *                License for more details.
25  *
26  *                The GNU General Public License should be included with
27  *                this file.  If not, you can view it at
28  *                http://www.gnu.org/copyleft/gpl.html
29  *                or write to the Free Software Foundation, Inc., 59
30  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
31  *
32  * Revisions   :
33  *    $Log$
34  *********************************************************************/
35
36 package org.privoxy.activityconsole;
37
38 /** 
39  * Utility string functions.
40  * @author Last Modified By: $Author$
41  * @version $Rev$-$Date$$State$
42  */
43 class StringUtil
44 {
45   private static final String
46     COPYRIGHT = org.privoxy.activityconsole.Copyright.COPYRIGHT;
47
48   /****************************************************************************
49   * This method replaces the preValue with the postValue in the originalString.
50   *
51   * @param originalString the string to have replacements
52   * @param preValue The substring value that the string currently contains
53   * @param postValue The substring value to replace the preValue
54   * @param recursive true if replacements should occur recursively
55   *
56   * @return String representing the substituted value(s)
57   ****************************************************************************/
58   public static final String replaceSubstring( String originalString
59                                                , String preValue
60                                                , String postValue
61                                                , boolean recursive
62                                              )
63   {
64     String finalString = originalString;
65     int previousFind = originalString.length();
66
67     int index = originalString.lastIndexOf(preValue);
68     while (index > -1        &&
69            index < previousFind
70           )
71     {
72       previousFind = index;
73
74       finalString = originalString.substring(0,index)
75                     + postValue
76                     + finalString.substring(index+preValue.length());
77
78       index = finalString.lastIndexOf(preValue,previousFind);
79       if (!recursive)
80       {
81         if (index == previousFind)
82           previousFind--;
83         index = finalString.lastIndexOf(preValue,previousFind);
84       }
85     }
86
87     return finalString;
88   }
89
90   /****************************************************************************
91   * This method replaces the preValue with the postValue in the originalString.
92   *
93   * @param originalString the string to have replacements
94   * @param preValue The substring value that the string currently contains
95   * @param postValue The substring value to replace the preValue
96   *
97   * @return String representing the substituted value(s)
98   ****************************************************************************/
99   public static final String replaceSubstring( String originalString
100                                                , String preValue
101                                                , String postValue
102                                              )
103   {
104     return(replaceSubstring(originalString, preValue, postValue, false));
105   }
106 }