|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ReplaceUtility.java | 50% | 93,8% | 66,7% | 80% |
|
||||||||||||||
| 1 | /* $Id: ReplaceUtility.java,v 1.6 2006/10/20 20:23:08 m31 Exp $ | |
| 2 | * | |
| 3 | * This file is part of the project "Hilbert II" - http://www.qedeq.org | |
| 4 | * | |
| 5 | * Copyright 2000-2006, Michael Meyling <mime@qedeq.org>. | |
| 6 | * | |
| 7 | * "Hilbert II" is free software; you can redistribute | |
| 8 | * it and/or modify it under the terms of the GNU General Public | |
| 9 | * License as published by the Free Software Foundation; either | |
| 10 | * version 2 of the License, or (at your option) any later version. | |
| 11 | * | |
| 12 | * This program is distributed in the hope that it will be useful, | |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 15 | * GNU General Public License for more details. | |
| 16 | */ | |
| 17 | ||
| 18 | package org.qedeq.kernel.utility; | |
| 19 | ||
| 20 | ||
| 21 | /** | |
| 22 | * A collection of useful static methods for string replacement. | |
| 23 | * | |
| 24 | * @version $Revision: 1.6 $ | |
| 25 | * @author Michael Meyling | |
| 26 | */ | |
| 27 | public final class ReplaceUtility { | |
| 28 | ||
| 29 | /** | |
| 30 | * Constructor, should never be called. | |
| 31 | */ | |
| 32 | 0 | private ReplaceUtility() { |
| 33 | // don't call me | |
| 34 | } | |
| 35 | ||
| 36 | /** | |
| 37 | * Replaces all occurrences of <code>search</code> in <code>text</code> | |
| 38 | * by <code>replace</code> and returns the result. | |
| 39 | * | |
| 40 | * @param text text to work on | |
| 41 | * @param search replace this text by <code>replace</code> | |
| 42 | * @param replace replacement for <code>search</code> | |
| 43 | * @return resulting string | |
| 44 | */ | |
| 45 | 133537 | public static String replace(final String text, |
| 46 | final String search, final String replace) { | |
| 47 | ||
| 48 | 133537 | final int len = search.length(); |
| 49 | 133537 | if (len == 0) { |
| 50 | 0 | return text; |
| 51 | } | |
| 52 | 133537 | final StringBuffer result = new StringBuffer(); |
| 53 | 133537 | int pos1 = 0; |
| 54 | 133537 | int pos2; |
| 55 | ? | while (0 <= (pos2 = text.indexOf(search, pos1))) { |
| 56 | 57477 | result.append(text.substring(pos1, pos2)); |
| 57 | 57477 | result.append(replace); |
| 58 | 57477 | pos1 = pos2 + len; |
| 59 | } | |
| 60 | 133537 | if (pos1 < text.length()) { |
| 61 | 128897 | result.append(text.substring(pos1)); |
| 62 | } | |
| 63 | 133537 | return result.toString(); |
| 64 | } | |
| 65 | ||
| 66 | ||
| 67 | /** | |
| 68 | * Replaces all occurrences of <code>search</code> in <code>text</code> | |
| 69 | * by <code>replace</code> and returns the result. | |
| 70 | * | |
| 71 | * @param text text to work on | |
| 72 | * @param search replace this text by <code>replace</code> | |
| 73 | * @param replace replacement for <code>search</code> | |
| 74 | */ | |
| 75 | 7417 | public static void replace(final StringBuffer text, |
| 76 | final String search, final String replace) { | |
| 77 | ||
| 78 | 7417 | final String result = replace(text.toString(), search, replace); |
| 79 | 7417 | text.setLength(0); |
| 80 | 7417 | text.append(result); |
| 81 | // TODO mime 20050205: check if the above could be replaced with: | |
| 82 | /* | |
| 83 | final StringBuffer result = new StringBuffer(); | |
| 84 | int pos1 = 0; | |
| 85 | int pos2; | |
| 86 | final int len = search.length(); | |
| 87 | while (0 <= (pos2 = text.indexOf(search, pos1))) { | |
| 88 | result.append(text.substring(pos1, pos2)); | |
| 89 | result.append(replace); | |
| 90 | pos1 = pos2 + len; | |
| 91 | } | |
| 92 | if (pos1 < text.length()) { | |
| 93 | result.append(text.substring(pos1)); | |
| 94 | } | |
| 95 | text.setLength(0); | |
| 96 | text.append(result); | |
| 97 | */ | |
| 98 | } | |
| 99 | ||
| 100 | ||
| 101 | } |
|
||||||||||