📄 perl5substitution.java
字号:
__addElement(_OPCODE_COPY); __addElement(offset); } if (next == subLength) { __addElement(next - offset); } continue; } if (offset >= 0) { __addElement(current - offset); offset = -1; } // Only do positional and escapes if we have a next char if (next == subLength) continue; nextc = subChars[next]; // Positional params if (c == '$') { saveDigits = __isInterpolationCharacter(nextc); } else if (c == '\\') { // Escape codes if (nextc == 'l') { if (!caseMode){ __addElement(_OPCODE_LOWERCASE_CHAR); current++; } } else if (nextc == 'u') { if (!caseMode) { __addElement(_OPCODE_UPPERCASE_CHAR); current++; } } else if (nextc == 'L') { __addElement(_OPCODE_LOWERCASE_MODE); current++; caseMode = true; } else if (nextc == 'U') { __addElement(_OPCODE_UPPERCASE_MODE); current++; caseMode = true; } else if (nextc == 'E') { __addElement(_OPCODE_ENDCASE_MODE); current++; caseMode = false; } else { escapeMode = true; } } } } String _finalInterpolatedSub(MatchResult result) { StringBuffer buffer = new StringBuffer(10); _calcSub(buffer, result); return buffer.toString(); } void _calcSub(StringBuffer buffer, MatchResult result) { int size, offset, count, caseMode; char[] sub, str, match; int[] subOpcodes = _subOpcodes; caseMode = 0; str = _substitutionChars; match = result.group(0).toCharArray(); size = _subOpcodesCount; for (int element = 0; element < size; element++) { int value = subOpcodes[element]; // If we have a group, set up interpolation, else // interpret op code. if(value >= 0 && value < result.groups()) { int end, len; offset = result.begin(value); if (offset < 0) continue; end = result.end(value); if (end < 0) continue; len = result.length(); if (offset >= len || end > len || offset >= end) continue; count = end - offset; sub = match; } else if (value == _OPCODE_COPY) { element++; if (element >= size) continue; offset = subOpcodes[element]; element++; if (element >= size) continue; count = subOpcodes[element]; sub = str; } else if (value == _OPCODE_LOWERCASE_CHAR || value == _OPCODE_UPPERCASE_CHAR) { if (caseMode != _OPCODE_LOWERCASE_MODE && caseMode != _OPCODE_UPPERCASE_MODE) caseMode = value; continue; } else if (value == _OPCODE_LOWERCASE_MODE || value == _OPCODE_UPPERCASE_MODE) { caseMode = value; continue; } else if (value == _OPCODE_ENDCASE_MODE) { caseMode = 0; continue; } else continue; // Apply modes to buf if (caseMode == _OPCODE_LOWERCASE_CHAR) { buffer.append(Character.toLowerCase(sub[offset++])); buffer.append(sub, offset, --count); caseMode = 0; } else if (caseMode == _OPCODE_UPPERCASE_CHAR) { buffer.append(Character.toUpperCase(sub[offset++])); buffer.append(sub, offset, --count); caseMode = 0; } else if (caseMode == _OPCODE_LOWERCASE_MODE) { for (int end = offset + count; offset < end; ) { buffer.append(Character.toLowerCase(sub[offset++])); } } else if (caseMode == _OPCODE_UPPERCASE_MODE) { for (int end = offset + count; offset < end; ) { buffer.append(Character.toUpperCase(sub[offset++])); } } else buffer.append(sub, offset, count); } } /** * Default constructor initializing substitution to a zero length * String and the number of interpolations to * {@link #INTERPOLATE_ALL}. */ public Perl5Substitution() { this("", INTERPOLATE_ALL); } /** * Creates a Perl5Substitution using the specified substitution * and setting the number of interpolations to * {@link #INTERPOLATE_ALL}. * <p> * @param substitution The string to use as a substitution. */ public Perl5Substitution(String substitution) { this(substitution, INTERPOLATE_ALL); } /** * Creates a Perl5Substitution using the specified substitution * and setting the number of interpolations to the specified value. * <p> * @param substitution The string to use as a substitution. * @param numInterpolations * If set to <b>INTERPOLATE_NONE</b>, interpolation variables are * interpreted literally and not as references to the saved * parenthesized groups of a pattern match. If set to * <b> INTERPOLATE_ALL </b>, all variable interpolations * are computed relative to the pattern match responsible for * the current substitution. If set to a positive integer, * the first <b> numInterpolations </b> substitutions have * their variable interpolation performed relative to the * most recent match, but the remaining substitutions have * their variable interpolations performed relative to the * <b> numInterpolations </b>'th match. */ public Perl5Substitution(String substitution, int numInterpolations) { setSubstitution(substitution, numInterpolations); } /** * Sets the substitution represented by this Perl5Substitution, also * setting the number of interpolations to * {@link #INTERPOLATE_ALL}. * You should use this method in order to avoid repeatedly allocating new * Perl5Substitutions. It is recommended that you allocate a single * Perl5Substitution and reuse it by using this method when appropriate. * <p> * @param substitution The string to use as a substitution. */ public void setSubstitution(String substitution) { setSubstitution(substitution, INTERPOLATE_ALL); } /** * Sets the substitution represented by this Perl5Substitution, also * setting the number of interpolations to the specified value. * You should use this method in order to avoid repeatedly allocating new * Perl5Substitutions. It is recommended that you allocate a single * Perl5Substitution and reuse it by using this method when appropriate. * <p> * @param substitution The string to use as a substitution. * @param numInterpolations * If set to <b>INTERPOLATE_NONE</b>, interpolation variables are * interpreted literally and not as references to the saved * parenthesized groups of a pattern match. If set to * <b> INTERPOLATE_ALL </b>, all variable interpolations * are computed relative to the pattern match responsible for * the current substitution. If set to a positive integer, * the first <b> numInterpolations </b> substitutions have * their variable interpolation performed relative to the * most recent match, but the remaining substitutions have * their variable interpolations performed relative to the * <b> numInterpolations </b>'th match. */ public void setSubstitution(String substitution, int numInterpolations) { super.setSubstitution(substitution); _numInterpolations = numInterpolations; if(numInterpolations != INTERPOLATE_NONE && (substitution.indexOf('$') != -1 || substitution.indexOf('\\') != -1)) __parseSubs(substitution); else _subOpcodes = null; _lastInterpolation = null; } /** * Appends the substitution to a buffer containing the original input * with substitutions applied for the pattern matches found so far. * See * {@link Substitution#appendSubstitution Substitution.appendSubstition()} * for more details regarding the expected behavior of this method. * <p> * @param appendBuffer The buffer containing the new string resulting * from performing substitutions on the original input. * @param match The current match causing a substitution to be made. * @param substitutionCount The number of substitutions that have been * performed so far by Util.substitute. * @param originalInput The original input upon which the substitutions are * being performed. This is a read-only parameter and is not modified. * @param matcher The PatternMatcher used to find the current match. * @param pattern The Pattern used to find the current match. */ public void appendSubstitution(StringBuffer appendBuffer, MatchResult match, int substitutionCount, PatternMatcherInput originalInput, PatternMatcher matcher, Pattern pattern) { if(_subOpcodes == null) { super.appendSubstitution(appendBuffer, match, substitutionCount, originalInput, matcher, pattern); return; } if(_numInterpolations < 1 || substitutionCount < _numInterpolations) _calcSub(appendBuffer, match); else { if(substitutionCount == _numInterpolations) _lastInterpolation = _finalInterpolatedSub(match); appendBuffer.append(_lastInterpolation); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -