Mercurial > vim
comparison src/eval.c @ 15:631143ac4a01 v7.0007
updated for version 7.0007
author | vimboss |
---|---|
date | Sat, 10 Jul 2004 09:47:34 +0000 |
parents | 946da5994c01 |
children | 3ba373b54370 |
comparison
equal
deleted
inserted
replaced
14:946da5994c01 | 15:631143ac4a01 |
---|---|
368 static void f_submatch __ARGS((VAR argvars, VAR retvar)); | 368 static void f_submatch __ARGS((VAR argvars, VAR retvar)); |
369 static void f_substitute __ARGS((VAR argvars, VAR retvar)); | 369 static void f_substitute __ARGS((VAR argvars, VAR retvar)); |
370 static void f_tempname __ARGS((VAR argvars, VAR retvar)); | 370 static void f_tempname __ARGS((VAR argvars, VAR retvar)); |
371 static void f_tolower __ARGS((VAR argvars, VAR retvar)); | 371 static void f_tolower __ARGS((VAR argvars, VAR retvar)); |
372 static void f_toupper __ARGS((VAR argvars, VAR retvar)); | 372 static void f_toupper __ARGS((VAR argvars, VAR retvar)); |
373 static void f_tr __ARGS((VAR argvars, VAR retvar)); | |
373 static void f_type __ARGS((VAR argvars, VAR retvar)); | 374 static void f_type __ARGS((VAR argvars, VAR retvar)); |
374 static void f_virtcol __ARGS((VAR argvars, VAR retvar)); | 375 static void f_virtcol __ARGS((VAR argvars, VAR retvar)); |
375 static void f_visualmode __ARGS((VAR argvars, VAR retvar)); | 376 static void f_visualmode __ARGS((VAR argvars, VAR retvar)); |
376 static void f_winbufnr __ARGS((VAR argvars, VAR retvar)); | 377 static void f_winbufnr __ARGS((VAR argvars, VAR retvar)); |
377 static void f_wincol __ARGS((VAR argvars, VAR retvar)); | 378 static void f_wincol __ARGS((VAR argvars, VAR retvar)); |
2921 {"synIDtrans", 1, 1, f_synIDtrans}, | 2922 {"synIDtrans", 1, 1, f_synIDtrans}, |
2922 {"system", 1, 1, f_system}, | 2923 {"system", 1, 1, f_system}, |
2923 {"tempname", 0, 0, f_tempname}, | 2924 {"tempname", 0, 0, f_tempname}, |
2924 {"tolower", 1, 1, f_tolower}, | 2925 {"tolower", 1, 1, f_tolower}, |
2925 {"toupper", 1, 1, f_toupper}, | 2926 {"toupper", 1, 1, f_toupper}, |
2927 {"tr", 3, 3, f_tr}, | |
2926 {"type", 1, 1, f_type}, | 2928 {"type", 1, 1, f_type}, |
2927 {"virtcol", 1, 1, f_virtcol}, | 2929 {"virtcol", 1, 1, f_virtcol}, |
2928 {"visualmode", 0, 1, f_visualmode}, | 2930 {"visualmode", 0, 1, f_visualmode}, |
2929 {"winbufnr", 1, 1, f_winbufnr}, | 2931 {"winbufnr", 1, 1, f_winbufnr}, |
2930 {"wincol", 0, 0, f_wincol}, | 2932 {"wincol", 0, 0, f_wincol}, |
7433 { | 7435 { |
7434 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */ | 7436 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */ |
7435 p++; | 7437 p++; |
7436 } | 7438 } |
7437 } | 7439 } |
7440 } | |
7441 | |
7442 /* | |
7443 * "tr(string, fromstr, tostr)" function | |
7444 */ | |
7445 static void | |
7446 f_tr(argvars, retvar) | |
7447 VAR argvars; | |
7448 VAR retvar; | |
7449 { | |
7450 char_u *instr; | |
7451 char_u *fromstr; | |
7452 char_u *tostr; | |
7453 char_u *p; | |
7454 #ifdef FEAT_MBYTE | |
7455 int inlen; | |
7456 int fromlen; | |
7457 int tolen; | |
7458 int idx; | |
7459 char_u *cpstr; | |
7460 int cplen; | |
7461 int first = TRUE; | |
7462 #endif | |
7463 char_u buf[NUMBUFLEN]; | |
7464 char_u buf2[NUMBUFLEN]; | |
7465 garray_T ga; | |
7466 | |
7467 instr = get_var_string(&argvars[0]); | |
7468 fromstr = get_var_string_buf(&argvars[1], buf); | |
7469 tostr = get_var_string_buf(&argvars[2], buf2); | |
7470 | |
7471 /* Default return value: empty string. */ | |
7472 retvar->var_type = VAR_STRING; | |
7473 retvar->var_val.var_string = NULL; | |
7474 ga_init2(&ga, (int)sizeof(char), 80); | |
7475 | |
7476 #ifdef FEAT_MBYTE | |
7477 if (!has_mbyte) | |
7478 #endif | |
7479 /* not multi-byte: fromstr and tostr must be the same length */ | |
7480 if (STRLEN(fromstr) != STRLEN(tostr)) | |
7481 { | |
7482 error: | |
7483 EMSG2(_(e_invarg2), fromstr); | |
7484 ga_clear(&ga); | |
7485 return; | |
7486 } | |
7487 | |
7488 /* fromstr and tostr have to contain the same number of chars */ | |
7489 while (*instr != NUL) | |
7490 { | |
7491 #ifdef FEAT_MBYTE | |
7492 if (has_mbyte) | |
7493 { | |
7494 inlen = mb_ptr2len_check(instr); | |
7495 cpstr = instr; | |
7496 cplen = inlen; | |
7497 idx = 0; | |
7498 for (p = fromstr; *p != NUL; p += fromlen) | |
7499 { | |
7500 fromlen = mb_ptr2len_check(p); | |
7501 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0) | |
7502 { | |
7503 for (p = tostr; *p != NUL; p += tolen) | |
7504 { | |
7505 tolen = mb_ptr2len_check(p); | |
7506 if (idx-- == 0) | |
7507 { | |
7508 cplen = tolen; | |
7509 cpstr = p; | |
7510 break; | |
7511 } | |
7512 } | |
7513 if (*p == NUL) /* tostr is shorter than fromstr */ | |
7514 goto error; | |
7515 break; | |
7516 } | |
7517 ++idx; | |
7518 } | |
7519 | |
7520 if (first && cpstr == instr) | |
7521 { | |
7522 /* Check that fromstr and tostr have the same number of | |
7523 * (multi-byte) characters. Done only once when a character | |
7524 * of instr doesn't appear in fromstr. */ | |
7525 first = FALSE; | |
7526 for (p = tostr; *p != NUL; p += tolen) | |
7527 { | |
7528 tolen = mb_ptr2len_check(p); | |
7529 --idx; | |
7530 } | |
7531 if (idx != 0) | |
7532 goto error; | |
7533 } | |
7534 | |
7535 ga_grow(&ga, cplen); | |
7536 mch_memmove(ga.ga_data + ga.ga_len, cpstr, (size_t)cplen); | |
7537 ga.ga_len += cplen; | |
7538 ga.ga_room -= cplen; | |
7539 | |
7540 instr += inlen; | |
7541 } | |
7542 else | |
7543 #endif | |
7544 { | |
7545 /* When not using multi-byte chars we can do it faster. */ | |
7546 p = vim_strchr(fromstr, *instr); | |
7547 if (p != NULL) | |
7548 ga_append(&ga, tostr[p - fromstr]); | |
7549 else | |
7550 ga_append(&ga, *instr); | |
7551 ++instr; | |
7552 } | |
7553 } | |
7554 | |
7555 retvar->var_val.var_string = ga.ga_data; | |
7438 } | 7556 } |
7439 | 7557 |
7440 /* | 7558 /* |
7441 * "type(expr)" function | 7559 * "type(expr)" function |
7442 */ | 7560 */ |