michael@0: /* michael@0: ********************************************************************** michael@0: * Copyright (c) 2001-2011, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ********************************************************************** michael@0: * Date Name Description michael@0: * 11/19/2001 aliu Creation. michael@0: ********************************************************************** michael@0: */ michael@0: michael@0: #include "unicode/unimatch.h" michael@0: #include "unicode/utf16.h" michael@0: #include "patternprops.h" michael@0: #include "util.h" michael@0: michael@0: // Define UChar constants using hex for EBCDIC compatibility michael@0: michael@0: static const UChar BACKSLASH = 0x005C; /*\*/ michael@0: static const UChar UPPER_U = 0x0055; /*U*/ michael@0: static const UChar LOWER_U = 0x0075; /*u*/ michael@0: static const UChar APOSTROPHE = 0x0027; // '\'' michael@0: static const UChar SPACE = 0x0020; // ' ' michael@0: michael@0: // "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" michael@0: static const UChar DIGITS[] = { michael@0: 48,49,50,51,52,53,54,55,56,57, michael@0: 65,66,67,68,69,70,71,72,73,74, michael@0: 75,76,77,78,79,80,81,82,83,84, michael@0: 85,86,87,88,89,90 michael@0: }; michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: UnicodeString& ICU_Utility::appendNumber(UnicodeString& result, int32_t n, michael@0: int32_t radix, int32_t minDigits) { michael@0: if (radix < 2 || radix > 36) { michael@0: // Bogus radix michael@0: return result.append((UChar)63/*?*/); michael@0: } michael@0: // Handle negatives michael@0: if (n < 0) { michael@0: n = -n; michael@0: result.append((UChar)45/*-*/); michael@0: } michael@0: // First determine the number of digits michael@0: int32_t nn = n; michael@0: int32_t r = 1; michael@0: while (nn >= radix) { michael@0: nn /= radix; michael@0: r *= radix; michael@0: --minDigits; michael@0: } michael@0: // Now generate the digits michael@0: while (--minDigits > 0) { michael@0: result.append(DIGITS[0]); michael@0: } michael@0: while (r > 0) { michael@0: int32_t digit = n / r; michael@0: result.append(DIGITS[digit]); michael@0: n -= digit * r; michael@0: r /= radix; michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: /** michael@0: * Return true if the character is NOT printable ASCII. michael@0: */ michael@0: UBool ICU_Utility::isUnprintable(UChar32 c) { michael@0: return !(c >= 0x20 && c <= 0x7E); michael@0: } michael@0: michael@0: /** michael@0: * Escape unprintable characters using \uxxxx notation for U+0000 to michael@0: * U+FFFF and \Uxxxxxxxx for U+10000 and above. If the character is michael@0: * printable ASCII, then do nothing and return FALSE. Otherwise, michael@0: * append the escaped notation and return TRUE. michael@0: */ michael@0: UBool ICU_Utility::escapeUnprintable(UnicodeString& result, UChar32 c) { michael@0: if (isUnprintable(c)) { michael@0: result.append(BACKSLASH); michael@0: if (c & ~0xFFFF) { michael@0: result.append(UPPER_U); michael@0: result.append(DIGITS[0xF&(c>>28)]); michael@0: result.append(DIGITS[0xF&(c>>24)]); michael@0: result.append(DIGITS[0xF&(c>>20)]); michael@0: result.append(DIGITS[0xF&(c>>16)]); michael@0: } else { michael@0: result.append(LOWER_U); michael@0: } michael@0: result.append(DIGITS[0xF&(c>>12)]); michael@0: result.append(DIGITS[0xF&(c>>8)]); michael@0: result.append(DIGITS[0xF&(c>>4)]); michael@0: result.append(DIGITS[0xF&c]); michael@0: return TRUE; michael@0: } michael@0: return FALSE; michael@0: } michael@0: michael@0: /** michael@0: * Returns the index of a character, ignoring quoted text. michael@0: * For example, in the string "abc'hide'h", the 'h' in "hide" will not be michael@0: * found by a search for 'h'. michael@0: */ michael@0: // FOR FUTURE USE. DISABLE FOR NOW for coverage reasons. michael@0: /* michael@0: int32_t ICU_Utility::quotedIndexOf(const UnicodeString& text, michael@0: int32_t start, int32_t limit, michael@0: UChar charToFind) { michael@0: for (int32_t i=start; i= pos); michael@0: //? michael@0: //? if (!isForward) { michael@0: //? --pos; // pos is a limit, so back up by one michael@0: //? } michael@0: //? michael@0: //? while (pos != stop && michael@0: //? PatternProps::isWhiteSpace(c = text.char32At(pos))) { michael@0: //? if (isForward) { michael@0: //? pos += U16_LENGTH(c); michael@0: //? } else { michael@0: //? pos -= U16_LENGTH(c); michael@0: //? } michael@0: //? } michael@0: //? michael@0: //? if (!isForward) { michael@0: //? ++pos; // make pos back into a limit michael@0: //? } michael@0: //? michael@0: //? return pos; michael@0: //?} michael@0: michael@0: /** michael@0: * Parse a single non-whitespace character 'ch', optionally michael@0: * preceded by whitespace. michael@0: * @param id the string to be parsed michael@0: * @param pos INPUT-OUTPUT parameter. On input, pos[0] is the michael@0: * offset of the first character to be parsed. On output, pos[0] michael@0: * is the index after the last parsed character. If the parse michael@0: * fails, pos[0] will be unchanged. michael@0: * @param ch the non-whitespace character to be parsed. michael@0: * @return true if 'ch' is seen preceded by zero or more michael@0: * whitespace characters. michael@0: */ michael@0: UBool ICU_Utility::parseChar(const UnicodeString& id, int32_t& pos, UChar ch) { michael@0: int32_t start = pos; michael@0: skipWhitespace(id, pos, TRUE); michael@0: if (pos == id.length() || michael@0: id.charAt(pos) != ch) { michael@0: pos = start; michael@0: return FALSE; michael@0: } michael@0: ++pos; michael@0: return TRUE; michael@0: } michael@0: michael@0: /** michael@0: * Parse a pattern string within the given Replaceable and a parsing michael@0: * pattern. Characters are matched literally and case-sensitively michael@0: * except for the following special characters: michael@0: * michael@0: * ~ zero or more Pattern_White_Space chars michael@0: * michael@0: * If end of pattern is reached with all matches along the way, michael@0: * pos is advanced to the first unparsed index and returned. michael@0: * Otherwise -1 is returned. michael@0: * @param pat pattern that controls parsing michael@0: * @param text text to be parsed, starting at index michael@0: * @param index offset to first character to parse michael@0: * @param limit offset after last character to parse michael@0: * @return index after last parsed character, or -1 on parse failure. michael@0: */ michael@0: int32_t ICU_Utility::parsePattern(const UnicodeString& pat, michael@0: const Replaceable& text, michael@0: int32_t index, michael@0: int32_t limit) { michael@0: int32_t ipat = 0; michael@0: michael@0: // empty pattern matches immediately michael@0: if (ipat == pat.length()) { michael@0: return index; michael@0: } michael@0: michael@0: UChar32 cpat = pat.char32At(ipat); michael@0: michael@0: while (index < limit) { michael@0: UChar32 c = text.char32At(index); michael@0: michael@0: // parse \s* michael@0: if (cpat == 126 /*~*/) { michael@0: if (PatternProps::isWhiteSpace(c)) { michael@0: index += U16_LENGTH(c); michael@0: continue; michael@0: } else { michael@0: if (++ipat == pat.length()) { michael@0: return index; // success; c unparsed michael@0: } michael@0: // fall thru; process c again with next cpat michael@0: } michael@0: } michael@0: michael@0: // parse literal michael@0: else if (c == cpat) { michael@0: index += U16_LENGTH(c); michael@0: ipat += U16_LENGTH(cpat); michael@0: if (ipat == pat.length()) { michael@0: return index; // success; c parsed michael@0: } michael@0: // fall thru; get next cpat michael@0: } michael@0: michael@0: // match failure of literal michael@0: else { michael@0: return -1; michael@0: } michael@0: michael@0: cpat = pat.char32At(ipat); michael@0: } michael@0: michael@0: return -1; // text ended before end of pat michael@0: } michael@0: michael@0: /** michael@0: * Append a character to a rule that is being built up. To flush michael@0: * the quoteBuf to rule, make one final call with isLiteral == TRUE. michael@0: * If there is no final character, pass in (UChar32)-1 as c. michael@0: * @param rule the string to append the character to michael@0: * @param c the character to append, or (UChar32)-1 if none. michael@0: * @param isLiteral if true, then the given character should not be michael@0: * quoted or escaped. Usually this means it is a syntactic element michael@0: * such as > or $ michael@0: * @param escapeUnprintable if true, then unprintable characters michael@0: * should be escaped using \uxxxx or \Uxxxxxxxx. These escapes will michael@0: * appear outside of quotes. michael@0: * @param quoteBuf a buffer which is used to build up quoted michael@0: * substrings. The caller should initially supply an empty buffer, michael@0: * and thereafter should not modify the buffer. The buffer should be michael@0: * cleared out by, at the end, calling this method with a literal michael@0: * character. michael@0: */ michael@0: void ICU_Utility::appendToRule(UnicodeString& rule, michael@0: UChar32 c, michael@0: UBool isLiteral, michael@0: UBool escapeUnprintable, michael@0: UnicodeString& quoteBuf) { michael@0: // If we are escaping unprintables, then escape them outside michael@0: // quotes. \u and \U are not recognized within quotes. The same michael@0: // logic applies to literals, but literals are never escaped. michael@0: if (isLiteral || michael@0: (escapeUnprintable && ICU_Utility::isUnprintable(c))) { michael@0: if (quoteBuf.length() > 0) { michael@0: // We prefer backslash APOSTROPHE to double APOSTROPHE michael@0: // (more readable, less similar to ") so if there are michael@0: // double APOSTROPHEs at the ends, we pull them outside michael@0: // of the quote. michael@0: michael@0: // If the first thing in the quoteBuf is APOSTROPHE michael@0: // (doubled) then pull it out. michael@0: while (quoteBuf.length() >= 2 && michael@0: quoteBuf.charAt(0) == APOSTROPHE && michael@0: quoteBuf.charAt(1) == APOSTROPHE) { michael@0: rule.append(BACKSLASH).append(APOSTROPHE); michael@0: quoteBuf.remove(0, 2); michael@0: } michael@0: // If the last thing in the quoteBuf is APOSTROPHE michael@0: // (doubled) then remove and count it and add it after. michael@0: int32_t trailingCount = 0; michael@0: while (quoteBuf.length() >= 2 && michael@0: quoteBuf.charAt(quoteBuf.length()-2) == APOSTROPHE && michael@0: quoteBuf.charAt(quoteBuf.length()-1) == APOSTROPHE) { michael@0: quoteBuf.truncate(quoteBuf.length()-2); michael@0: ++trailingCount; michael@0: } michael@0: if (quoteBuf.length() > 0) { michael@0: rule.append(APOSTROPHE); michael@0: rule.append(quoteBuf); michael@0: rule.append(APOSTROPHE); michael@0: quoteBuf.truncate(0); michael@0: } michael@0: while (trailingCount-- > 0) { michael@0: rule.append(BACKSLASH).append(APOSTROPHE); michael@0: } michael@0: } michael@0: if (c != (UChar32)-1) { michael@0: /* Since spaces are ignored during parsing, they are michael@0: * emitted only for readability. We emit one here michael@0: * only if there isn't already one at the end of the michael@0: * rule. michael@0: */ michael@0: if (c == SPACE) { michael@0: int32_t len = rule.length(); michael@0: if (len > 0 && rule.charAt(len-1) != c) { michael@0: rule.append(c); michael@0: } michael@0: } else if (!escapeUnprintable || !ICU_Utility::escapeUnprintable(rule, c)) { michael@0: rule.append(c); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Escape ' and '\' and don't begin a quote just for them michael@0: else if (quoteBuf.length() == 0 && michael@0: (c == APOSTROPHE || c == BACKSLASH)) { michael@0: rule.append(BACKSLASH); michael@0: rule.append(c); michael@0: } michael@0: michael@0: // Specials (printable ascii that isn't [0-9a-zA-Z]) and michael@0: // whitespace need quoting. Also append stuff to quotes if we are michael@0: // building up a quoted substring already. michael@0: else if (quoteBuf.length() > 0 || michael@0: (c >= 0x0021 && c <= 0x007E && michael@0: !((c >= 0x0030/*'0'*/ && c <= 0x0039/*'9'*/) || michael@0: (c >= 0x0041/*'A'*/ && c <= 0x005A/*'Z'*/) || michael@0: (c >= 0x0061/*'a'*/ && c <= 0x007A/*'z'*/))) || michael@0: PatternProps::isWhiteSpace(c)) { michael@0: quoteBuf.append(c); michael@0: // Double ' within a quote michael@0: if (c == APOSTROPHE) { michael@0: quoteBuf.append(c); michael@0: } michael@0: } michael@0: michael@0: // Otherwise just append michael@0: else { michael@0: rule.append(c); michael@0: } michael@0: } michael@0: michael@0: void ICU_Utility::appendToRule(UnicodeString& rule, michael@0: const UnicodeString& text, michael@0: UBool isLiteral, michael@0: UBool escapeUnprintable, michael@0: UnicodeString& quoteBuf) { michael@0: for (int32_t i=0; itoPattern(pat, escapeUnprintable), michael@0: TRUE, escapeUnprintable, quoteBuf); michael@0: } michael@0: } michael@0: michael@0: U_NAMESPACE_END