void LivePropertyEditorBase::findOriginalValueInCode() { CodeDocument::Position pos (document, value.sourceLine, 0); String line (pos.getLineText()); String::CharPointerType p (line.getCharPointer()); p = CharacterFunctions::find (p, CharPointer_ASCII ("JUCE_LIVE_CONSTANT")); if (p.isEmpty()) { // Not sure how this would happen - some kind of mix-up between source code and line numbers.. jassertfalse; return; } p += (int) (sizeof ("JUCE_LIVE_CONSTANT") - 1); p = p.findEndOfWhitespace(); if (! CharacterFunctions::find (p, CharPointer_ASCII ("JUCE_LIVE_CONSTANT")).isEmpty()) { // Aargh! You've added two JUCE_LIVE_CONSTANT macros on the same line! // They're identified by their line number, so you must make sure each // one goes on a separate line! jassertfalse; } if (p.getAndAdvance() == '(') { String::CharPointerType start (p), end (p); int depth = 1; while (! end.isEmpty()) { const juce_wchar c = end.getAndAdvance(); if (c == '(') ++depth; if (c == ')') --depth; if (depth == 0) { --end; break; } } if (end > start) { valueStart = CodeDocument::Position (document, value.sourceLine, (int) (start - line.getCharPointer())); valueEnd = CodeDocument::Position (document, value.sourceLine, (int) (end - line.getCharPointer())); valueStart.setPositionMaintained (true); valueEnd.setPositionMaintained (true); wasHex = String (start, end).containsIgnoreCase ("0x"); } } }
void XmlDocument::skipNextWhiteSpace() { for (;;) { input = input.findEndOfWhitespace(); if (input.isEmpty()) { outOfData = true; break; } if (*input == '<') { if (input[1] == '!' && input[2] == '-' && input[3] == '-') { input += 4; const int closeComment = input.indexOf (CharPointer_ASCII ("-->")); if (closeComment < 0) { outOfData = true; break; } input += closeComment + 3; continue; } if (input[1] == '?') { input += 2; const int closeBracket = input.indexOf (CharPointer_ASCII ("?>")); if (closeBracket < 0) { outOfData = true; break; } input += closeBracket + 2; continue; } } break; } }
bool XmlDocument::parseDTD() { if (CharacterFunctions::compareUpTo (input, CharPointer_ASCII ("<!DOCTYPE"), 9) == 0) { input += 9; const String::CharPointerType dtdStart (input); for (int n = 1; n > 0;) { const juce_wchar c = readNextChar(); if (outOfData) return false; if (c == '<') ++n; else if (c == '>') --n; } dtdText = String (dtdStart, input - 1).trim(); } return true; }
bool isReservedKeyword (String::CharPointerType token, const int tokenLength) noexcept { static const char* const keywords2Char[] = { "if", "do", "or", "id", 0 }; static const char* const keywords3Char[] = { "for", "int", "new", "try", "xor", "and", "asm", "not", 0 }; static const char* const keywords4Char[] = { "bool", "void", "this", "true", "long", "else", "char", "enum", "case", "goto", "auto", 0 }; static const char* const keywords5Char[] = { "while", "bitor", "break", "catch", "class", "compl", "const", "false", "float", "short", "throw", "union", "using", "or_eq", 0 }; static const char* const keywords6Char[] = { "return", "struct", "and_eq", "bitand", "delete", "double", "extern", "friend", "inline", "not_eq", "public", "sizeof", "static", "signed", "switch", "typeid", "wchar_t", "xor_eq", 0}; static const char* const keywordsOther[] = { "const_cast", "continue", "default", "explicit", "mutable", "namespace", "operator", "private", "protected", "register", "reinterpret_cast", "static_cast", "template", "typedef", "typename", "unsigned", "virtual", "volatile", "@implementation", "@interface", "@end", "@synthesize", "@dynamic", "@public", "@private", "@property", "@protected", "@class", 0 }; const char* const* k; switch (tokenLength) { case 2: k = keywords2Char; break; case 3: k = keywords3Char; break; case 4: k = keywords4Char; break; case 5: k = keywords5Char; break; case 6: k = keywords6Char; break; default: if (tokenLength < 2 || tokenLength > 16) return false; k = keywordsOther; break; } int i = 0; while (k[i] != 0) { if (token.compare (CharPointer_ASCII (k[i])) == 0) return true; ++i; } return false; }
static bool isReservedKeyword (String::CharPointerType token, const int tokenLength) noexcept { static const char* const keywords2Char[] = { "if", "or", "in", "do", nullptr }; static const char* const keywords3Char[] = { "and", "end", "for", "nil", "not", nullptr }; static const char* const keywords4Char[] = { "then", "true", "else", nullptr }; static const char* const keywords5Char[] = { "false", "local", "until", "while", "break", nullptr }; static const char* const keywords6Char[] = { "repeat", "return", "elseif", nullptr}; static const char* const keywordsOther[] = { "function", "@interface", "@end", "@synthesize", "@dynamic", "@public", "@private", "@property", "@protected", "@class", nullptr }; const char* const* k; switch (tokenLength) { case 2: k = keywords2Char; break; case 3: k = keywords3Char; break; case 4: k = keywords4Char; break; case 5: k = keywords5Char; break; case 6: k = keywords6Char; break; default: if (tokenLength < 2 || tokenLength > 16) return false; k = keywordsOther; break; } for (int i = 0; k[i] != 0; ++i) if (token.compare (CharPointer_ASCII (k[i])) == 0) return true; return false; }
void XmlDocument::readEntity (String& result) { // skip over the ampersand ++input; if (input.compareIgnoreCaseUpTo (CharPointer_ASCII ("amp;"), 4) == 0) { input += 4; result += '&'; } else if (input.compareIgnoreCaseUpTo (CharPointer_ASCII ("quot;"), 5) == 0) { input += 5; result += '"'; } else if (input.compareIgnoreCaseUpTo (CharPointer_ASCII ("apos;"), 5) == 0) { input += 5; result += '\''; } else if (input.compareIgnoreCaseUpTo (CharPointer_ASCII ("lt;"), 3) == 0) { input += 3; result += '<'; } else if (input.compareIgnoreCaseUpTo (CharPointer_ASCII ("gt;"), 3) == 0) { input += 3; result += '>'; } else if (*input == '#') { int charCode = 0; ++input; if (*input == 'x' || *input == 'X') { ++input; int numChars = 0; while (input[0] != ';') { const int hexValue = CharacterFunctions::getHexDigitValue (input[0]); if (hexValue < 0 || ++numChars > 8) { setLastError ("illegal escape sequence", true); break; } charCode = (charCode << 4) | hexValue; ++input; } ++input; } else if (input[0] >= '0' && input[0] <= '9') { int numChars = 0; while (input[0] != ';') { if (++numChars > 12) { setLastError ("illegal escape sequence", true); break; } charCode = charCode * 10 + ((int) input[0] - '0'); ++input; } ++input; } else { setLastError ("illegal escape sequence", true); result += '&'; return; } result << (juce_wchar) charCode; } else { const String::CharPointerType entityNameStart (input); const int closingSemiColon = input.indexOf ((juce_wchar) ';'); if (closingSemiColon < 0) { outOfData = true; result += '&'; } else { input += closingSemiColon + 1; result += expandExternalEntity (String (entityNameStart, (size_t) closingSemiColon)); } } }
void XmlDocument::readChildElements (XmlElement& parent) { LinkedListPointer<XmlElement>::Appender childAppender (parent.firstChildElement); for (;;) { const String::CharPointerType preWhitespaceInput (input); skipNextWhiteSpace(); if (outOfData) { setLastError ("unmatched tags", false); break; } if (*input == '<') { const juce_wchar c1 = input[1]; if (c1 == '/') { // our close tag.. const int closeTag = input.indexOf ((juce_wchar) '>'); if (closeTag >= 0) input += closeTag + 1; break; } if (c1 == '!' && CharacterFunctions::compareUpTo (input + 2, CharPointer_ASCII ("[CDATA["), 7) == 0) { input += 9; const String::CharPointerType inputStart (input); for (;;) { const juce_wchar c0 = *input; if (c0 == 0) { setLastError ("unterminated CDATA section", false); outOfData = true; break; } else if (c0 == ']' && input[1] == ']' && input[2] == '>') { childAppender.append (XmlElement::createTextElement (String (inputStart, input))); input += 3; break; } ++input; } } else { // this is some other element, so parse and add it.. if (XmlElement* const n = readNextElement (true)) childAppender.append (n); else break; } } else // must be a character block { input = preWhitespaceInput; // roll back to include the leading whitespace MemoryOutputStream textElementContent; bool contentShouldBeUsed = ! ignoreEmptyTextElements; for (;;) { const juce_wchar c = *input; if (c == '<') { if (input[1] == '!' && input[2] == '-' && input[3] == '-') { input += 4; const int closeComment = input.indexOf (CharPointer_ASCII ("-->")); if (closeComment < 0) { setLastError ("unterminated comment", false); outOfData = true; return; } input += closeComment + 3; continue; } break; } if (c == 0) { setLastError ("unmatched tags", false); outOfData = true; return; } if (c == '&') { String entity; readEntity (entity); if (entity.startsWithChar ('<') && entity [1] != 0) { const String::CharPointerType oldInput (input); const bool oldOutOfData = outOfData; input = entity.getCharPointer(); outOfData = false; while (XmlElement* n = readNextElement (true)) childAppender.append (n); input = oldInput; outOfData = oldOutOfData; } else { textElementContent << entity; contentShouldBeUsed = contentShouldBeUsed || entity.containsNonWhitespaceChars(); } } else { for (;; ++input) { juce_wchar nextChar = *input; if (nextChar == '\r') { nextChar = '\n'; if (input[1] == '\n') continue; } if (nextChar == '<' || nextChar == '&') break; if (nextChar == 0) { setLastError ("unmatched tags", false); outOfData = true; return; } textElementContent.appendUTF8Char (nextChar); contentShouldBeUsed = contentShouldBeUsed || ! CharacterFunctions::isWhitespace (nextChar); } } } if (contentShouldBeUsed) childAppender.append (XmlElement::createTextElement (textElementContent.toUTF8())); } } }
void XmlDocument::readChildElements (XmlElement* parent) { LinkedListPointer<XmlElement>::Appender childAppender (parent->firstChildElement); for (;;) { const String::CharPointerType preWhitespaceInput (input); skipNextWhiteSpace(); if (outOfData) { setLastError ("unmatched tags", false); break; } if (*input == '<') { const juce_wchar c1 = input[1]; if (c1 == '/') { // our close tag.. const int closeTag = input.indexOf ((juce_wchar) '>'); if (closeTag >= 0) input += closeTag + 1; break; } else if (c1 == '!' && CharacterFunctions::compareUpTo (input + 2, CharPointer_ASCII ("[CDATA["), 7) == 0) { input += 9; const String::CharPointerType inputStart (input); for (;;) { const juce_wchar c0 = *input; if (c0 == 0) { setLastError ("unterminated CDATA section", false); outOfData = true; break; } else if (c0 == ']' && input[1] == ']' && input[2] == '>') { childAppender.append (XmlElement::createTextElement (String (inputStart, input))); input += 3; break; } ++input; } } else { // this is some other element, so parse and add it.. if (XmlElement* const n = readNextElement (true)) childAppender.append (n); else break; } } else // must be a character block { input = preWhitespaceInput; // roll back to include the leading whitespace String textElementContent; for (;;) { const juce_wchar c = *input; if (c == '<') break; if (c == 0) { setLastError ("unmatched tags", false); outOfData = true; return; } if (c == '&') { String entity; readEntity (entity); if (entity.startsWithChar ('<') && entity [1] != 0) { const String::CharPointerType oldInput (input); const bool oldOutOfData = outOfData; input = entity.getCharPointer(); outOfData = false; for (;;) { XmlElement* const n = readNextElement (true); if (n == nullptr) break; childAppender.append (n); } input = oldInput; outOfData = oldOutOfData; } else { textElementContent += entity; } } else { const String::CharPointerType start (input); for (;;) { const juce_wchar nextChar = *input; if (nextChar == '<' || nextChar == '&') break; if (nextChar == 0) { setLastError ("unmatched tags", false); outOfData = true; return; } ++input; } textElementContent.appendCharPointer (start, input); } } if ((! ignoreEmptyTextElements) || textElementContent.containsNonWhitespaceChars()) childAppender.append (XmlElement::createTextElement (textElementContent)); } } }