| 1 | package main |
| 2 | |
| 3 | import "strings" |
| 4 | |
| 5 | // The following was copied from the crypto/openpgpg/packet package. |
| 6 | |
| 7 | // The original license can be found at https://git.io/vFFwQ |
| 8 | // |
| 9 | // Copyright (c) 2009 The Go Authors. All rights reserved. |
| 10 | // |
| 11 | // Redistribution and use in source and binary forms, with or without |
| 12 | // modification, are permitted provided that the following conditions are |
| 13 | // met: |
| 14 | // |
| 15 | // * Redistributions of source code must retain the above copyright |
| 16 | // notice, this list of conditions and the following disclaimer. |
| 17 | // * Redistributions in binary form must reproduce the above |
| 18 | // copyright notice, this list of conditions and the following disclaimer |
| 19 | // in the documentation and/or other materials provided with the |
| 20 | // distribution. |
| 21 | // * Neither the name of Google Inc. nor the names of its |
| 22 | // contributors may be used to endorse or promote products derived from |
| 23 | // this software without specific prior written permission. |
| 24 | // |
| 25 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 26 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 27 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 28 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 29 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 30 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 31 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 32 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 33 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 34 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 35 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 36 | |
| 37 | // The orignal code can be found at https://git.io/vFFwX |
| 38 | // |
| 39 | // parseUserID extracts the name, comment and email from a user id string that |
| 40 | // is formatted as "Full Name (Comment) <[email protected]>". |
| 41 | func parseUserID(id string) (name, comment, email string) { |
| 42 | var n, c, e struct { |
| 43 | start, end int |
| 44 | } |
| 45 | var state int |
| 46 | |
| 47 | for offset, rune := range id { |
| 48 | switch state { |
| 49 | case 0: |
| 50 | // Entering name |
| 51 | n.start = offset |
| 52 | state = 1 |
| 53 | fallthrough |
| 54 | case 1: |
| 55 | // In name |
| 56 | if rune == '(' { |
| 57 | state = 2 |
| 58 | n.end = offset |
| 59 | } else if rune == '<' { |
| 60 | state = 5 |
| 61 | n.end = offset |
| 62 | } |
| 63 | case 2: |
| 64 | // Entering comment |
| 65 | c.start = offset |
| 66 | state = 3 |
| 67 | fallthrough |
| 68 | case 3: |
| 69 | // In comment |
| 70 | if rune == ')' { |
| 71 | state = 4 |
| 72 | c.end = offset |
| 73 | } |
| 74 | case 4: |
| 75 | // Between comment and email |
| 76 | if rune == '<' { |
| 77 | state = 5 |
| 78 | } |
| 79 | case 5: |
| 80 | // Entering email |
| 81 | e.start = offset |
| 82 | state = 6 |
| 83 | fallthrough |
| 84 | case 6: |
| 85 | // In email |
| 86 | if rune == '>' { |
| 87 | state = 7 |
| 88 | e.end = offset |
| 89 | } |
| 90 | default: |
| 91 | // After email |
| 92 | } |
| 93 | } |
| 94 | switch state { |
| 95 | case 1: |
| 96 | // ended in the name |
| 97 | n.end = len(id) |
| 98 | case 3: |
| 99 | // ended in comment |
| 100 | c.end = len(id) |
| 101 | case 6: |
| 102 | // ended in email |
| 103 | e.end = len(id) |
| 104 | } |
| 105 | |
| 106 | name = strings.TrimSpace(id[n.start:n.end]) |
| 107 | comment = strings.TrimSpace(id[c.start:c.end]) |
| 108 | email = strings.TrimSpace(id[e.start:e.end]) |
| 109 | return |
| 110 | } |