How to detect non-GSM character in Golang?

31 Views Asked by At

I have a code to determine Text parameter using pdutext.GSM7 or pdutext.UCS2 like here :

sm, err := smpp_transmitter.Submit(&smpp.ShortMessage{
        Src:      data_map[`sender_masking`],
        Dst:      data_map[`msisdn`],
        Text:     pdutext.GSM7(data_map[`sms`]),
        Register: pdufield.NoDeliveryReceipt,
})

my goal is to detect if data_map[sms] contains non-GSM character, it will using pdutext.UCS2, otherwise will use pdutext.GSM7. i have mid solution by listing all character in condition. but i tought there will be efficient way to do that

1

There are 1 best solutions below

0
levniko On

I think you can use regex. May be like this:

func isGSM7(s string) bool {
    // Define a regex pattern for GSM 7-bit characters
    gsm7Pattern := "^[A-Za-z0-9@£$¥èéùìòÇØøÅå_ÆæßÉ!\"#%&'()*+,-./:;<=>?¡ÄÖÑܧ¿äöñüà^{}\\[~\\]|\\€]*$"
    
    matched, _ := regexp.MatchString(gsm7Pattern, s)
    return matched
}