bool AXmlElement::isNameEquals(const AString& nameattr) const { size_t pos = nameattr.find('@'); if (AConstant::npos == pos) return m_Name.equals(nameattr); else { AString name; AString attr; nameattr.peek(name, 0, pos); nameattr.peek(attr, pos); return (m_Name.equals(name) && m_Attributes.exists(attr)); } }
void ATextGenerator::generateRandomString(AOutputBuffer& target, size_t len, const AString& strCharSet) { for (size_t i = 0x0; i < len; ++i) { target.append(strCharSet.peek(ARandomNumberGenerator::get().nextRange(strCharSet.getSize()))); } }
int AWordUtility::splitSyllables(const AString& source, VECTOR_AString& result, bool handleSilentTrailing /* = true */) { if (source.isEmpty()) return 0; AString word(source); if (handleSilentTrailing) word.stripTrailing(msstr_SilentEnd); int count = 0; size_t start = 0; size_t pos = word.findOneOf(msstr_SyllableSeparators); while (AConstant::npos != pos) { AString str; word.peek(str, start, pos - start + 1); result.push_back(str.c_str()); ++count; start = pos+1; pos = word.findNotOneOf(msstr_SyllableSeparators, start); if (AConstant::npos != pos) pos = word.findOneOf(msstr_SyllableSeparators, start); } if (start < word.getSize()) { //a_Append leftovers to last word AString str; word.peek(str, start); if (count > 0) result.back().append(str); else result.push_back(str); } //a_Account for the silent trailing letters if (handleSilentTrailing && word.getSize() < source.getSize()) { AString str; source.peek(str, word.getSize()); if (count > 0) result.back().append(str); else result.push_back(str); } return count; }
void AWordUtility::getPlural(const AString& one, AString& many) { many.clear(); //a_Words of size 1 or 2, just append s and return if (one.getSize() < 3) { many.assign(one); many.append('s'); return; } switch(one.last()) { case 's': { char c = one.at(one.getSize()-2); if ('i' == c) { //a_"is" -> "es" many.assign(one); many.set('i', many.getSize()-2); } else if ('u' == c) { //a_"us" -> "ii" one.peek(many, 0, one.getSize()-2); many.append("ii", 2); } else { many.assign(one); many.append("es", 2); } } break; case 'z': case 'x': many.assign(one); many.append("es", 2); break; case 'h': { char c = one.at(one.getSize()-2); if ('s' == c || 'c' == c) { many.assign(one); many.append("es", 2); } else { many.assign(one); many.append('s'); } } break; case 'y': { char c = one.at(one.getSize()-2); if (AConstant::npos != sstr_Vowels.find(c)) { //a_vowel+'y', add 's' many.assign(one); many.append('s'); } else { //a_consonant+'y', convert 'y' to 'ies' one.peek(many, 0, one.getSize()-1); many.append("ies", 3); } } break; default: many.assign(one); many.append('s'); break; } }
void ATemplateNodeHandler_DADA::Node::_appendWordType(ADadaDataHolder *pddh, MAP_AString_AString& globals, const AString& strType, AOutputBuffer& target) { //a_First remove control tags "TYPE:controltag1,controltag2,..." AString strTypeName, strControl; LIST_AString listControlNames; size_t pos = strType.find(':'); if (AConstant::npos != pos) { strType.peek(strTypeName, 0, pos); strTypeName.makeLower(); strType.peek(strControl, pos+1); strControl.makeLower(); strControl.split(listControlNames, ',', AConstant::ASTRING_WHITESPACE); } else { strTypeName.assign(strType); strTypeName.makeLower(); } ADadaDataHolder::WORDMAP::iterator it = pddh->useWordMap().find(strTypeName); if (pddh->useWordMap().end() != it) { VECTOR_AString& vec = (*it).second; u4 index = ARandomNumberGenerator::get().nextRange((int)vec.size()); AString strSaveVariable; AString str(vec.at(index)); //a_Apply control code actions LIST_AString::iterator itN = listControlNames.begin(); while (itN != listControlNames.end()) { if ('$' == (*itN).at(0, '\x0')) { //a_Add variable to global map (*itN).peek(strSaveVariable, 1); globals[strSaveVariable] = str; strSaveVariable.clear(); } else if (!strTypeName.compare("verb")) { //a_Verb only control if (!(*itN).compare("present", 7)) { if (AConstant::npos != AWordUtility::sstr_Vowels.find(str.last())) { //a_Vowel at the end is removed and replaced with "ing" str.rremove(1); } str.append("ing", 3); } else if (!(*itN).compare("past", 4)) { if (AConstant::npos == AWordUtility::sstr_Vowels.find(str.last())) { str.append("ed", 2); } else { str.append("d", 1); } } } if (!(*itN).compare("article", 7)) { AString strTemp(str); if (AConstant::npos == AWordUtility::sstr_Vowels.find(str.at(0))) { str.assign("a ", 2); } else { str.assign("an ", 3); } str.append(strTemp); } if (!(*itN).compare("plural", 6)) { AString strTemp; AWordUtility::getPlural(str, strTemp); str.assign(strTemp); } if (!(*itN).compare("uppercase", 9)) { str.makeUpper(); } if (!(*itN).compare("lowercase", 9)) { str.makeLower(); } if (!(*itN).compare("proper", 6)) { str.use(0) = (u1)toupper(str.at(0)); size_t nextStart; size_t nextSpace = str.find(' ', 1); while (AConstant::npos != nextSpace) { nextStart = nextSpace + 1; if (nextStart < str.getSize()) str.use(nextStart) = (u1)toupper(str.at(nextStart)); else break; nextSpace = str.find(' ', nextStart); } } ++itN; } target.append(str); } else { //a_Unknown type, pass through as is. target.append('%'); target.append(strType); target.append('%'); } }
void ATemplateNodeHandler_DADA::Node::_appendVariable(ADadaDataHolder *pddh, MAP_AString_AString& globals, const AString& strType, AOutputBuffer& target) { AASSERT(this, strType.getSize() > 0); AString strTypeName, strControl; LIST_AString listControlNames; size_t pos = strType.find(':'); if (AConstant::npos != pos) { strType.peek(strTypeName, 0, pos); strTypeName.makeLower(); strType.peek(strControl, pos+1); strControl.makeLower(); strControl.split(listControlNames, ',', AConstant::ASTRING_WHITESPACE); } else { strTypeName.assign(strType); strTypeName.makeLower(); } //a_Find it in the global lookup MAP_AString_AString::iterator it = globals.find(strTypeName); if (it != globals.end()) { AString str((*it).second); LIST_AString::iterator itN = listControlNames.begin(); while (itN != listControlNames.end()) { if (!(*itN).compare("article", 7)) { AString strTemp(str); if (AConstant::npos == AWordUtility::sstr_Vowels.find(str.at(0))) { str.assign("a ", 2); } else { str.assign("an ", 3); } str.append(strTemp); } if (!(*itN).compare("plural", 6)) { AString strTemp; AWordUtility::getPlural(str, strTemp); str.assign(strTemp); } if (!(*itN).compare("uppercase", 9)) { str.makeUpper(); } if (!(*itN).compare("lowercase", 9)) { str.makeLower(); } if (!(*itN).compare("proper", 6)) { str.use(0) = (u1)toupper(str.at(0)); size_t nextStart; size_t nextSpace = str.find(' ', 1); while (AConstant::npos != nextSpace) { nextStart = nextSpace + 1; if (nextStart < str.getSize()) str.use(nextStart) = (u1)toupper(str.at(nextStart)); else break; nextSpace = str.find(' ', nextStart); } } ++itN; } target.append(str); } else { //a_Not found in global map } }
size_t AFragmentString::parse(const AString& str) { size_t iRet = AConstant::npos; size_t iX = 0; AString strHold; while(iX < str.getSize()) { switch(str[iX]) { case '\\' : { ++iX; if (str.getSize() > iX) { strHold += str[iX]; iRet = iX; } else { //a_String truncated iRet = iX - 1; return iRet; } } break; case '(' : { iX++; if (str.getSize() > iX) { size_t iF = str.find(')', iX+1); if (iF != AConstant::npos) { AASSERT(this, iF >= iX); if (!strHold.isEmpty()) { m_Container.push_back(new AFragmentConstant(strHold)); strHold.clear(); iRet = iX-1; } AString strT; str.peek(strT, iX, iF-iX); iX = iF; // advance offset after extraction u1 b; u4 n[2]; b = (u1)strT.toInt(); if (b > 9) b = 9; if ((iF = strT.find(',')) == AConstant::npos) { // Only 1 number, the digit value long stop = 10; for (int i=1; i<b; ++i) stop *= 10; m_Container.push_back(new AFragmentCounter(b, u4(stop-1))); } else { AString str1; strT.peek(str1, iF + 1); if ((iF = str1.find(',')) == AConstant::npos) { n[0] = str1.toU4(); m_Container.push_back(new AFragmentCounter(b, n[0])); } else { AString strQ; str1.peek(strQ, 0, iF); n[0] = strQ.toU4(); strQ.clear(); str1.peek(strQ, iF+1); n[1] = strQ.toU4(); if ((iF = str1.find(',', iF+1)) == AConstant::npos) m_Container.push_back(new AFragmentCounter(b, n[0], n[1])); else { strQ.clear(); str1.peek(strQ, iF+1); m_Container.push_back(new AFragmentCounter(b, n[0], n[1], strQ.toInt())); } } } iRet = iX; } else { //a_Closing tag not found iRet = iX-1; return iRet; } } else { //a_String is truncated iRet = iX-1; return iRet; } } break; case '{' : { ++iX; if (str.getSize() > iX) { size_t iF = str.find('}', iX+1); if (!strHold.isEmpty()) { m_Container.push_back(new AFragmentConstant(strHold)); strHold.clear(); iRet = iX-1;} if (iF != AConstant::npos) { AASSERT(this, iF >= iX); AString strT; str.peek(strT, iX, iF-iX); char cX; while (!strT.isEmpty()) { cX = strT.get(); switch (cX) { case '#': m_Container.push_back(new AFragmentSet(AFragmentSet::Numeric)); break; case '&': m_Container.push_back(new AFragmentSet(AFragmentSet::LowercaseAlpha)); break; case '@': m_Container.push_back(new AFragmentSet(AFragmentSet::UppercaseAlpha)); break; case '%': m_Container.push_back(new AFragmentSet(AFragmentSet::LowercaseAlphaNumeric)); break; case '^': m_Container.push_back(new AFragmentSet(AFragmentSet::UppercaseAlphaNumeric)); break; case '?': m_Container.push_back(new AFragmentSet(AFragmentSet::AlphaNumeric)); break; default : strHold += cX; } } iX = iF; iRet = iX; } else { //a_Closing tag not found iRet = iX-1; return iRet; } } else { //a_String is truncated iRet = iX-1; return iRet; } } break; case '[' : { size_t iF = str.find(']', iX+1); if (iF != AConstant::npos) { AASSERT(this, iF >= iX); if (!strHold.isEmpty()) { m_Container.push_back(new AFragmentConstant(strHold)); strHold.clear(); } AString strT; str.peek(strT, iX + 1, iF - iX - 1); m_Container.push_back(new AFragmentSet(strT)); iX = iF; iRet = iX; } else { //a_Closing tag not found, return iRet at point of error iRet = iX; return iRet; } } break; case '<' : { size_t iF = str.find('>', iX+1); if (iF != AConstant::npos) { AASSERT(this, iF >= iX); if (!strHold.isEmpty()) { m_Container.push_back(new AFragmentConstant(strHold)); strHold.clear(); } AString strT; str.peek(strT, iX + 1, iF - iX - 1); m_Container.push_back(new AFragmentOdometer(strT.toSize_t())); iX = iF; iRet = iX; } else { //a_Closing tag not found, return iRet at point of error iRet = iX; return iRet; } } break; default : strHold += str[iX]; iRet = iX; } iX++; } if (!strHold.isEmpty()) { m_Container.push_back(new AFragmentConstant(strHold)); } //a_If we got here we have succeeded iRet = iX; m_Finished = false; return iRet; }