|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.daffodil.util |
| 18 | + |
| 19 | +/** |
| 20 | + * A abstract base for Remappers which convert strings. |
| 21 | + * |
| 22 | + * The public interface is just `def remap(s: String): String`. |
| 23 | + * |
| 24 | + * There are protected methods that implementations must provide. |
| 25 | + * |
| 26 | + * Contains shared implementation methods also. |
| 27 | + * |
| 28 | + * NOTE: This is inner loop stuff. Keep it and derived classes lean and fast. |
| 29 | + * Use a java-like coding style. While loops, not map/flatmap/etc. avoid tuples. |
| 30 | + */ |
| 31 | +trait CharacterSetRemapper { |
| 32 | + |
| 33 | + /** |
| 34 | + * Remaps the string. Returns the original string object if no remapping is required. |
| 35 | + */ |
| 36 | + def remap(s: String): String = remapImpl(s) |
| 37 | + |
| 38 | + /** |
| 39 | + * Remaps 1 character, does not consider any context. |
| 40 | + */ |
| 41 | + def remapChar(c: Char): Char = remap(0, c, 0).toChar |
| 42 | + |
| 43 | + /** |
| 44 | + * Remaps characters. Provides the previous and following characters since some remappings |
| 45 | + * require this context. |
| 46 | + * |
| 47 | + * Plays a trick with negating the return value in order to avoid having to |
| 48 | + * return more than one value, which is potentially less efficient. |
| 49 | + * |
| 50 | + * @param prev The character prior to the one being considered. (Needed for surrogates) |
| 51 | + * @param curr The character under consideration for remapping. |
| 52 | + * @param next The next character afterwards. (Needed for surrogates and CRLF pairs) |
| 53 | + * @return The remapped character (as an Int) or that same remapped character Int |
| 54 | + * value negated, which signals that curr+next was remapped to a single character. |
| 55 | + * Such as is needed if CRLF is remapped to just LF. |
| 56 | + */ |
| 57 | + protected def remap (prev: Int, curr: Int, next: Int): Int |
| 58 | + |
| 59 | + private def needsRemapping(s: String): Boolean = { |
| 60 | + // a one liner in scala, |
| 61 | + // |
| 62 | + // `s.exists{ remapChar(_) != _ }` |
| 63 | + // |
| 64 | + // but we need a fast java-like while loop... |
| 65 | + var pos = 0 |
| 66 | + var c = 0.toChar |
| 67 | + val len = s.length |
| 68 | + if (len != 0) |
| 69 | + while (pos < len) { |
| 70 | + c = s(pos) |
| 71 | + if (remapChar(c) != c) |
| 72 | + return true |
| 73 | + pos += 1 |
| 74 | + } |
| 75 | + false |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Scan first to avoid allocating a new string when remapping is |
| 80 | + * not needed, which is the common case. |
| 81 | + * This is worth it to save the allocation overhead. |
| 82 | + */ |
| 83 | + private def remapImpl(s: String) = if (needsRemapping(s)) remapLoop(s) else s |
| 84 | + |
| 85 | + /** |
| 86 | + * Because of surrogate pairs, and the difference between 16-bit string codepoints |
| 87 | + * and real character codes, lots of things that traverse strings need |
| 88 | + * to consider either the codepoint after (if current is a leading surrogate) |
| 89 | + * or codepoint before (if current is a trailing surrogate). |
| 90 | + * |
| 91 | + * This algorithm uses a StringBuilder which is not synchronized |
| 92 | + * so it is noticably faster than StringBuffer, and since the StringBuilder |
| 93 | + * is local to the function, we don't have to worry about any threading issues. |
| 94 | + * This makes for a noticeable speed increase. |
| 95 | + */ |
| 96 | + private def remapLoop(s: String): String = { |
| 97 | + |
| 98 | + val len = s.length |
| 99 | + if (len == 0) return s |
| 100 | + |
| 101 | + val sb = new StringBuilder() |
| 102 | + |
| 103 | + var pos = 0; |
| 104 | + var prev = 0 |
| 105 | + var curr = s(0).toInt |
| 106 | + var next = 0 |
| 107 | + var newCurr = 0 // positive normally, but will be negative if we're to skip a char |
| 108 | + |
| 109 | + while (pos < len) { |
| 110 | + next = if (pos + 1 < len) s(pos + 1) else 0 |
| 111 | + // |
| 112 | + // sign of newCurr is negative if we're to skip 1 character |
| 113 | + // such as if the prior iteration collapsed a CRLF to just LF. |
| 114 | + // |
| 115 | + if (newCurr >= 0) { |
| 116 | + newCurr = remap(prev, curr, next) |
| 117 | + sb.append( |
| 118 | + if (newCurr < 0) { |
| 119 | + // |
| 120 | + // if newCurr is negative, it's still the replacement |
| 121 | + // remapped character code, just negated to indicate need to skip |
| 122 | + // |
| 123 | + (-newCurr).toChar |
| 124 | + } |
| 125 | + else newCurr.toChar |
| 126 | + ) |
| 127 | + prev = curr |
| 128 | + } |
| 129 | + curr = next |
| 130 | + pos += 1 |
| 131 | + } |
| 132 | + |
| 133 | + sb.toString |
| 134 | + } |
| 135 | +} |
0 commit comments