Provided a name and a list of items, find the first item that matches the name Will first provide a pair of starts-width and exact matches, and if not found then a contains.
(itemName string, items ...Item)
| 579 | // Will first provide a pair of starts-width and exact matches, |
| 580 | // and if not found then a contains. |
| 581 | func FindMatchIn(itemName string, items ...Item) (pMatch Item, fMatch Item) { |
| 582 | |
| 583 | if len(itemName) > 1 { |
| 584 | if itemName[0] == '!' { // Special meaning to specify an item |
| 585 | |
| 586 | var itemIdMatch int = 0 |
| 587 | var itemUUIDMatch uuid.UUID = uuid.UUID{} |
| 588 | |
| 589 | parts := strings.Split(itemName[1:], `:`) |
| 590 | itemIdMatch, _ = strconv.Atoi(parts[0]) |
| 591 | |
| 592 | if len(parts) > 1 { |
| 593 | itemUUIDMatch, _ = uuid.FromString(parts[1]) |
| 594 | } |
| 595 | |
| 596 | for _, itm := range items { |
| 597 | |
| 598 | // If a uid was included, it takes priority over qualifying/disqualifying |
| 599 | if !itemUUIDMatch.IsNil() { |
| 600 | if itm.UUID != itemUUIDMatch { |
| 601 | continue |
| 602 | } |
| 603 | return itm, itm |
| 604 | } |
| 605 | |
| 606 | if itemIdMatch > 0 { |
| 607 | if itm.ItemId != itemIdMatch { |
| 608 | continue |
| 609 | } |
| 610 | return itm, itm |
| 611 | } |
| 612 | } |
| 613 | return Item{}, Item{} |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | itemName, itemNumber := util.GetMatchNumber(itemName) |
| 618 | |
| 619 | var matchItem Item |
| 620 | var closeMatchItem Item |
| 621 | |
| 622 | var matchItemCt int = 0 |
| 623 | var closeMatchItemCt int = 0 |
| 624 | |
| 625 | for _, i := range items { |
| 626 | |
| 627 | part, full := i.NameMatch(itemName, false) |
| 628 | |
| 629 | if part { |
| 630 | closeMatchItemCt++ |
| 631 | if closeMatchItemCt == itemNumber { |
| 632 | closeMatchItem = i |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | if full { |
| 637 | matchItemCt++ |
| 638 | if matchItemCt == itemNumber { |