The import path a file binds to `alias` (an explicit alias, else the * path's last segment, Go's usual package name); "" when none. */
| 439 | /* The import path a file binds to `alias` (an explicit alias, else the |
| 440 | * path's last segment, Go's usual package name); "" when none. */ |
| 441 | static void op_import_path(const char *text, const char *alias, char *out, size_t out_sz) { |
| 442 | out[0] = '\0'; |
| 443 | size_t al = strlen(alias); |
| 444 | for (const char *p = strstr(text, "import"); p; p = strstr(p + SKIP_ONE, "import")) { |
| 445 | if ((p > text && p[-SKIP_ONE] != '\n') || op_ident_char(p[OP_SLEN("import")])) { |
| 446 | continue; |
| 447 | } |
| 448 | const char *q = p + OP_SLEN("import"); |
| 449 | const char *end = q; |
| 450 | while (*end == ' ' || *end == '\t') { |
| 451 | end++; |
| 452 | } |
| 453 | end = *end == '(' ? strchr(end, ')') : strchr(end, '\n'); |
| 454 | end = end ? end : q + strlen(q); |
| 455 | /* each import line: [alias] "path" */ |
| 456 | for (const char *l = q; l < end;) { |
| 457 | while (l < end && (*l == ' ' || *l == '\t' || *l == '(' || *l == '\n' || *l == '\r')) { |
| 458 | l++; |
| 459 | } |
| 460 | const char *name = l; |
| 461 | while (l < end && op_ident_char(*l)) { |
| 462 | l++; |
| 463 | } |
| 464 | size_t nl = (size_t)(l - name); |
| 465 | while (l < end && (*l == ' ' || *l == '\t')) { |
| 466 | l++; |
| 467 | } |
| 468 | if (l >= end || *l != '"') { |
| 469 | while (l < end && *l != '\n') { |
| 470 | l++; |
| 471 | } |
| 472 | continue; |
| 473 | } |
| 474 | const char *ps = l + SKIP_ONE; |
| 475 | const char *pe = strchr(ps, '"'); |
| 476 | if (!pe || pe > end) { |
| 477 | break; |
| 478 | } |
| 479 | const char *seg = pe; |
| 480 | while (seg > ps && seg[-SKIP_ONE] != '/') { |
| 481 | seg--; |
| 482 | } |
| 483 | bool hit = nl ? (nl == al && strncmp(name, alias, al) == 0) |
| 484 | : ((size_t)(pe - seg) == al && strncmp(seg, alias, al) == 0); |
| 485 | if (hit) { |
| 486 | snprintf(out, out_sz, "%.*s", (int)(pe - ps), ps); |
| 487 | return; |
| 488 | } |
| 489 | l = pe + SKIP_ONE; |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | /* Is directory `dir` the package at import path `path`: the path ends with |
| 495 | * the directory, on a segment boundary (the module path is its prefix). */ |
no test coverage detected