Example #1
0
U_CAPI UTransliterator * U_EXPORT2
utrans_openU(const UChar * id,
             int32_t idLength,
             UTransDirection dir,
             const UChar * rules,
             int32_t rulesLength,
             UParseError * parseError,
             UErrorCode * status)
{
	if (status == NULL || U_FAILURE(*status))
	{
		return NULL;
	}
	if (id == NULL)
	{
		*status = U_ILLEGAL_ARGUMENT_ERROR;
		return NULL;
	}
	UParseError temp;

	if (parseError == NULL)
	{
		parseError = &temp;
	}

	UnicodeString ID(idLength < 0, id, idLength); // r-o alias

	if (rules == NULL)
	{

		Transliterator * trans = NULL;

		trans = Transliterator::createInstance(ID, dir, *parseError, *status);

		if (U_FAILURE(*status))
		{
			return NULL;
		}
		return (UTransliterator *) trans;
	}
	else
	{
		UnicodeString ruleStr(rulesLength < 0,
		                      rules,
		                      rulesLength); // r-o alias

		Transliterator * trans = NULL;
		trans = Transliterator::createFromRules(ID, ruleStr, dir, *parseError, *status);
		if (U_FAILURE(*status))
		{
			return NULL;
		}

		return (UTransliterator *) trans;
	}
}
Example #2
0
void RuleParser::parse() {
	sweet::jsonparser jp(str);

	std::string tokStr("Token");
	if(jp.getRoot()->pathExists(tokStr)) {
		auto tok = jp.getRoot()->access(tokStr);
		auto tokType = tok->getType();
		ASSERT_EQ(tokType, sweet::value::type_array);

		for(auto& it : tok->getArray()) {
			std::string name = it->getObject()->access("Name")->getString();
			std::string regex = it->getObject()->access("Regex")->getString();
			std::string convertFunc;
			if(it->getObject()->pathExists("ConvertFunction")) {
				convertFunc = it->getObject()->access("ConvertFunction")->getString();
			}
			token.insert(std::make_pair(name,
				Token(name, regex, convertFunc)
			));
		}
	}

	std::string ruleStr("Rules");
	if(jp.getRoot()->pathExists(ruleStr)) {
		auto rls = jp.getRoot()->access(ruleStr);
		auto rlsType = rls->getType();
		ASSERT_EQ(rlsType, sweet::value::type_array);
		for(auto& it : rls->getArray()) {

			std::string ruleName = it->getObject()->access("Name")->getString();
			auto& entry = it->getObject()->access("Expression")->getArray();
			for(auto& jt : entry) {
				RuleVector rv;
				auto semiSplit = split(jt->getObject()->access("Rule")->getString(), ';');
				std::string ruleEnd = jt->getObject()->get<std::string>("Id", "");
				for(auto& ht : semiSplit) {
					std::string type = trim(ht.substr(0, ht.find('(')));
					size_t lParen = ht.find('(');
					size_t rParen = ht.find(')');
					std::string saveName;
					if(lParen != std::string::npos && rParen != std::string::npos) {
						saveName = trim(ht.substr(lParen+1, rParen), "\t ()");
					}

					rv.push_back(RulePart(type, saveName, ruleEnd));
				}

				ruleMap.insert(std::make_pair(ruleName, Expr(rv)));
			}

		}
	}
}