-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
13 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,17 @@ | ||
package tygo | ||
|
||
import ( | ||
"log" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func isProbablyIotaType(groupType string) bool { | ||
groupType = strings.Trim(groupType, "()") | ||
return groupType == "iota" || strings.HasPrefix(groupType, "iota +") || strings.HasSuffix(groupType, "+ iota") | ||
} | ||
|
||
// Note: this could be done so much more elegantly, but this probably covers 99.9% of iota usecases | ||
func basicIotaOffsetValueParse(groupType string) int { | ||
if !isProbablyIotaType(groupType) { | ||
panic("can't parse non-iota type") | ||
} | ||
groupType = strings.Trim(groupType, "()") | ||
if groupType == "iota" { | ||
return 0 | ||
} | ||
parts := strings.Split(groupType, " + ") | ||
var iotaRegexp = regexp.MustCompile(`\biota\b`) | ||
|
||
var numPart string | ||
if parts[0] == "iota" { | ||
numPart = parts[1] | ||
} else { | ||
numPart = parts[0] | ||
} | ||
func isProbablyIotaType(valueString string) bool { | ||
return !strings.ContainsAny(valueString, "\"'`") && iotaRegexp.MatchString(valueString) | ||
} | ||
|
||
addValue, err := strconv.ParseInt(numPart, 10, 64) | ||
if err != nil { | ||
log.Panicf("Failed to guesstimate initial iota value for \"%s\": %v", groupType, err) | ||
} | ||
return int(addValue) | ||
func replaceIotaValue(valueString string, iotaValue int) string { | ||
return iotaRegexp.ReplaceAllLiteralString(valueString, strconv.Itoa(iotaValue)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters