|
|
@ -1,6 +1,83 @@ |
|
|
|
package bitmask |
|
|
|
|
|
|
|
import "unsafe" |
|
|
|
import ( |
|
|
|
"regexp" |
|
|
|
"strconv" |
|
|
|
"strings" |
|
|
|
"unsafe" |
|
|
|
) |
|
|
|
|
|
|
|
type DataTypeType string |
|
|
|
|
|
|
|
const ( |
|
|
|
DTTint DataTypeType = "int" |
|
|
|
DTTdint DataTypeType = "dint" |
|
|
|
DTTreal DataTypeType = "real" |
|
|
|
DTTdatetime DataTypeType = "date_and_time" |
|
|
|
DTTdate DataTypeType = "date" |
|
|
|
DTTtime DataTypeType = "time" |
|
|
|
DTTbool DataTypeType = "bool" |
|
|
|
DTTbyte DataTypeType = "byte" |
|
|
|
DTTchar DataTypeType = "char" |
|
|
|
DTTword DataTypeType = "word" |
|
|
|
DTTdword DataTypeType = "dword" |
|
|
|
) |
|
|
|
|
|
|
|
func ParseHexForByteWordAndDWord(hs string) (int, error) { |
|
|
|
var a int64 |
|
|
|
|
|
|
|
// B#16#FF
|
|
|
|
// W#16#FFFF
|
|
|
|
// DW#16#FFFF FFFF oder DW#16#FFFFFFFF
|
|
|
|
// auch gültig: 16#89ABCDEF
|
|
|
|
|
|
|
|
hs = strings.ToLower( //2. bitte nur in Kleinbuchstaben, das vermindert die Komplexität des Regulären Ausdrucks
|
|
|
|
strings.ReplaceAll(hs, " ", "")) //1. entferne alle Leerzeichen
|
|
|
|
|
|
|
|
re, err := regexp.Compile(`^((b|w|dw)#)?(((16#)([0-9a-f]{1,8})|([0-1]{1,8})){1})$`) |
|
|
|
|
|
|
|
d := re.FindStringSubmatch(hs) |
|
|
|
|
|
|
|
if d[6] != "" { |
|
|
|
a, _ = strconv.ParseInt(d[6], 16, 64) |
|
|
|
} else if d[7] != "" { |
|
|
|
a, _ = strconv.ParseInt(d[7], 2, 64) |
|
|
|
} /*else { |
|
|
|
panic("I should not happen") |
|
|
|
}*/ |
|
|
|
//fmt.Println(a, d[6], d[7])
|
|
|
|
|
|
|
|
return int(a), err |
|
|
|
} |
|
|
|
|
|
|
|
func MeasureSPSTypeLength(s DataTypeType) int8 { |
|
|
|
switch strings.ToLower(string(s)) { |
|
|
|
case string(DTTint): |
|
|
|
return 2 |
|
|
|
case string(DTTdint): |
|
|
|
return 4 |
|
|
|
case string(DTTreal): |
|
|
|
return 4 |
|
|
|
case string(DTTdatetime): |
|
|
|
return 8 |
|
|
|
case string(DTTdate): // Nicht verifiziert
|
|
|
|
return 2 |
|
|
|
case string(DTTtime): // Nicht verifiziert
|
|
|
|
return 4 |
|
|
|
case string(DTTbool): |
|
|
|
return 1 |
|
|
|
case string(DTTchar): |
|
|
|
return 1 |
|
|
|
case string(DTTbyte): |
|
|
|
return 1 |
|
|
|
case string(DTTword): // Nicht verifiziert
|
|
|
|
return 2 |
|
|
|
case string(DTTdword): // Nicht verifiziert
|
|
|
|
return 4 |
|
|
|
default: // Nur im Fehlerfall
|
|
|
|
return -1 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Sets the bit at pos in the integer n. pos = 0 is first.
|
|
|
|
func SetBit(n int, pos uint) int { |
|
|
|