! \brief Replace in replacement tokens \\d with substrings of string pointed by * pmatch. */
| 37 | * pmatch. |
| 38 | */ |
| 39 | int replace(regmatch_t* pmatch, char* string, char* replacement, str* result) |
| 40 | { |
| 41 | int len, i, j, digit, size; |
| 42 | |
| 43 | len = strlen(replacement); |
| 44 | j = 0; |
| 45 | |
| 46 | for (i = 0; i < len; i++) { |
| 47 | if (replacement[i] == '\\') { |
| 48 | if (i < len - 1) { |
| 49 | if (isdigit((unsigned char)replacement[i+1])) { |
| 50 | digit = replacement[i+1] - '0'; |
| 51 | if (pmatch[digit].rm_so != -1) { |
| 52 | size = pmatch[digit].rm_eo - pmatch[digit].rm_so; |
| 53 | if (j + size < result->len) { |
| 54 | memcpy(&(result->s[j]), string+pmatch[digit].rm_so, size); |
| 55 | j = j + size; |
| 56 | } else { |
| 57 | return -1; |
| 58 | } |
| 59 | } else { |
| 60 | return -2; |
| 61 | } |
| 62 | i = i + 1; |
| 63 | continue; |
| 64 | } else { |
| 65 | i = i + 1; |
| 66 | } |
| 67 | } else { |
| 68 | return -3; |
| 69 | } |
| 70 | } |
| 71 | if (j + 1 < result->len) { |
| 72 | result->s[j] = replacement[i]; |
| 73 | j = j + 1; |
| 74 | } else { |
| 75 | return -4; |
| 76 | } |
| 77 | } |
| 78 | result->len = j; |
| 79 | return 1; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | /*! \brief Match pattern against string and store result in pmatch */ |
no outgoing calls
no test coverage detected