int del_substr( char *str, char const *substr ) { char *pstr = NULL; char const *psubstr = NULL; while( *str != NUL ) { if( is_substr( str, substr) ) break; str++; } if ( *str != NUL ) { pstr = str; psubstr = substr; while ( *substr != NUL ) { str++; substr++; } } else return 0; while ( *pstr++ = *str++ ); return 1; }
// script_english_number_* String do_english_num(String input, String(*fun)(int)) { if (is_substr(input, 0, _("<param-"))) { // a keyword parameter, of the form "<param->123</param->" size_t start = skip_tag(input, 0); if (start != String::npos) { size_t end = input.find_first_of(_('<'), start); if (end != String::npos) { String is = input.substr(start, end - start); long i = 0; if (is.ToLong(&i)) { if (i == 1) { return _("<hint-1>") + substr_replace(input, start, end, fun(i)); } else { return _("<hint-2>") + substr_replace(input, start, end, fun(i)); } } } } return _("<hint-2>") + input; } else { long i = 0; if (input.ToLong(&i)) { return fun(i); } return input; } }
/** * @return 0:OK -1:not found */ int find_file(const char *sha1_input, char *matched_filename) { int ret = -1; validate_sha1(sha1_input); char sha1_input_firsrt2chars[2]; const char *sha1_input_from3rd = &sha1_input[2]; //printf("3rd = %s\n", sha1_input_from3rd); sha1_input_firsrt2chars[0] = sha1_input[0]; sha1_input_firsrt2chars[1] = sha1_input[1]; sha1_input_firsrt2chars[2] = 0; //printf("sha1 first2 = %s\n", sha1_input_firsrt2chars); char dir[256] = ".git/objects/"; DIR *dp; struct dirent *entry; struct stat statbuf; strcat(dir, sha1_input_firsrt2chars); if(( dp = opendir(dir) ) == NULL ){ perror(dir); fprintf(stderr, "sha1_input = %s\n", sha1_input); exit( EXIT_FAILURE ); } while((entry = readdir(dp)) != NULL){ stat(entry->d_name, &statbuf); if ((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0)) { continue; } else { //printf("entry->d_name = %s\n", entry->d_name); //ユーザ入力のsha1とファイル名を比較して、 //前者が後者の先頭部分一致すればそれが目的のオブジェクトであるとみなす。 if (is_substr(sha1_input_from3rd, entry->d_name)) { // match! //printf("match :%s\n", entry->d_name ); strcpy(matched_filename, dir); strcat(matched_filename, "/"); strcat(matched_filename, entry->d_name); //printf("matched_filename = %s\n", matched_filename); //filename = matched_filename; ret = 0; break; } } } closedir(dp); return ret; }
// script_english_singular/plural/singplur String do_english(String input, String(*fun)(const String&)) { if (is_substr(input, 0, _("<param-"))) { // a keyword parameter, of the form "<param->123</param->" size_t start = skip_tag(input, 0); if (start != String::npos) { size_t end = input.find_first_of(_('<'), start); if (end != String::npos) { String is = input.substr(start, end - start); return substr_replace(input, start, end, fun(is)); } } return input; // failed } else { return fun(input); } }
void Keyword::prepare(const vector<KeywordParamP>& param_types, bool force) { if (!force && !match_re.empty()) return; parameters.clear(); // Prepare regex String regex; String text; // normal, non-regex, text vector<KeywordParamP>::const_iterator param = parameters.begin(); #if USE_CASE_INSENSITIVE_KEYWORDS regex = _("(?i)"); // case insensitive matching #endif // Parse the 'match' string for (size_t i = 0 ; i < match.size() ;) { Char c = match.GetChar(i); if (is_substr(match, i, _("<atom-param"))) { // parameter, determine type... size_t start = skip_tag(match, i), end = match_close_tag(match, i); String type = match.substr(start, end-start); // find parameter type 'type' KeywordParamP param; FOR_EACH_CONST(pt, param_types) { if (pt->name == type) { param = pt; break; } } if (!param) { // throwing an error can mean a set will not be loaded! // instead, simply disable the keyword //throw InternalError(_("Unknown keyword parameter type: ") + type); handle_error(_("Unknown keyword parameter type: ") + type); valid = false; return; } parameters.push_back(param); // modify regex : match text before param->compile(); // remove the separator from the text to prevent duplicates param->eat_separator_before(text); regex += _("(") + regex_escape(text) + _(")"); text.clear(); // modify regex : match parameter regex += _("(") + make_non_capturing(param->match) + (param->optional ? _(")?") : _(")")); i = skip_tag(match, end); // eat separator_after? param->eat_separator_after(match, i); } else {
String english_singular(const String& str) { if (str.size() > 3 && is_substr(str, str.size()-3, _("ies"))) { return str.substr(0, str.size() - 3) + _("y"); } else if (str.size() > 3 && is_substr(str, str.size()-3, _("oes"))) { return str.substr(0, str.size() - 2); } else if (str.size() > 4 && is_substr(str, str.size()-4, _("ches"))) { return str.substr(0, str.size() - 2); } else if (str.size() > 4 && is_substr(str, str.size()-4, _("shes"))) { return str.substr(0, str.size() - 2); } else if (str.size() > 4 && is_substr(str, str.size()-4, _("sses"))) { return str.substr(0, str.size() - 2); } else if (str.size() > 5 && is_substr(str, str.size()-3, _("ves")) && (is_substr(str, str.size()-5, _("el")) || is_substr(str, str.size()-5, _("ar")) )) { return str.substr(0, str.size() - 3) + _("f"); } else if (str.size() > 1 && str.GetChar(str.size() - 1) == _('s')) { return str.substr(0, str.size() - 1); } else if (str.size() >= 3 && is_substr(str, str.size()-3, _("men"))) { return str.substr(0, str.size() - 2) + _("an"); } else { return str; } }
void MultipleChoiceValue::normalForm() { String val = value->toString(); // which choices are active? vector<bool> seen(field().choices->lastId()); for (size_t pos = 0 ; pos < val.size() ; ) { if (val.GetChar(pos) == _(' ')) { ++pos; // ingore whitespace } else { // does this choice match the one asked about? size_t end = val.find_first_of(_(','), pos); if (end == String::npos) end = val.size(); // find this choice for (size_t i = 0 ; i < seen.size() ; ++i) { if (is_substr(val, pos, field().choices->choiceName((int)i))) { seen[i] = true; break; } } pos = end + 1; } } // now put them back in the right order val.clear(); for (size_t i = 0 ; i < seen.size() ; ++i) { if (seen[i]) { if (!val.empty()) val += _(", "); val += field().choices->choiceName((int)i); } } // empty choice name if (val.empty()) { val = field().empty_choice; } // store value = with_defaultness_of(value, to_script(val)); }
String capitalize(const String& s) { String result = s; bool after_space = true; FOR_EACH_IT(it, result) { if (*it == _(' ') || *it == _('/')) { after_space = true; } else if (after_space) { after_space = false; // See http://trac.wxwidgets.org/ticket/12594 //if (it != s.begin() && if (s.begin() != it && (is_substr(result,it,_("is ")) || is_substr(result,it,_("the ")) || is_substr(result,it,_("in ")) || is_substr(result,it,_("of ")) || is_substr(result,it,_("to ")) || is_substr(result,it,_("at ")) || is_substr(result,it,_("a " )))) { // Short words are not capitalized, keep lower case } else { *it = toUpper(*it); } } } return result; }
/** A hint is formed by * 1. an insertion of a parameter, <param-..>...</param->. * 2. a <hint-1> or <hint-2> tag * * Hints have the following meaning: * - "<hint-1>xxx(yyy)zzz" ---> "xxxzzz" (singular) * - "<hint-2>xxx(yyy)zzz" ---> "xxxyyyzzz" (plural) * - "[^., ]a <param-..>[aeiou]" ---> "\1 an \2" (articla 'an', case insensitive) * - "<hint-?>" ---> "" (remove <hint>s afterwards) * * Note: there is no close tags for hints */ String process_english_hints(const String& str) { String ret; ret.reserve(str.size()); // have we seen a <hint-1/2>? // 1 for singular, 2 for plural int singplur = 0; for (size_t i = 0 ; i < str.size() ; ) { Char c = str.GetChar(i); if (is_substr(str, i, _("<hint-"))) { Char h = str.GetChar(i + 6); // hint code if (h == _('1')) { singplur = 1; } else if (h == _('2')) { singplur = 2; } i = skip_tag(str, i); } else if (is_substr(str, i, _("<param-"))) { size_t after = skip_tag(str, i); if (after != String::npos) { Char c = str.GetChar(after); if (singplur == 1 && is_substr(str, after, _("a</param-"))) { // a -> an, where the a originates from english_number_a(1) size_t after_end = skip_tag(str,after+1); if (after_end == String::npos) { throw Error(_("Incomplete </param> tag")); } if (after_end != String::npos && after_end + 1 < str.size() && isSpace(str.GetChar(after_end)) && is_vowel(str.GetChar(after_end+1))) { ret.append(str,i,after-i); ret += _("an"); i = after + 1; continue; } } else if (is_vowel(c) && ret.size() >= 2) { // a -> an? // is there "a" before this? String last = ret.substr(ret.size() - 2); if ( (ret.size() == 2 || !isAlpha(ret.GetChar(ret.size() - 3))) && (last == _("a ") || last == _("A ")) ) { ret.insert(ret.size() - 1, _('n')); } } else if (is_substr(str, after, _("</param-")) && ret.size() >= 1 && ret.GetChar(ret.size() - 1) == _(' ')) { // empty param, drop space before it ret.resize(ret.size() - 1); } } ret.append(str,i,min(after,str.size())-i); i = after; } else if (is_substr(str, i, _("<singular>"))) { // singular -> keep, plural -> drop size_t start = skip_tag(str, i); size_t end = match_close_tag(str, start); if (singplur == 1 && end != String::npos) { ret += str.substr(start, end - start); } singplur = 0; i = skip_tag(str, end); } else if (is_substr(str, i, _("<plural>"))) { // singular -> drop, plural -> keep size_t start = skip_tag(str, i); size_t end = match_close_tag(str, start); if (singplur == 2 && end != String::npos) { ret += str.substr(start, end - start); } singplur = 0; i = skip_tag(str, end); } else if (c == _('(') && singplur) { // singular -> drop (...), plural -> keep it size_t end = str.find_first_of(_(')'), i); if (end != String::npos) { if (singplur == 2) { ret += str.substr(i + 1, end - i - 1); } i = end + 1; } else { // handle like normal ret += c; ++i; } singplur = 0; } else if (c == _('<')) { size_t after = skip_tag(str, i); ret.append(str,i,min(after,str.size())-i); i = after; } else { ret += c; ++i; } } return ret; }