Last active
August 29, 2015 14:26
-
-
Save ik5/805f775feb85265c3b67 to your computer and use it in GitHub Desktop.
Calculating how many SMS fragments will a message have
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
package main | |
import ( | |
"fmt" | |
"math" | |
"os" | |
"unicode/utf8" | |
) | |
func calculateSmsFragments(message string) uint64 { | |
mb_len := utf8.RuneCountInString(message) | |
byte_len := len(message) | |
max_message_length := 160 | |
max_multi_message_length := 153 | |
if mb_len != byte_len { | |
max_message_length = 70 | |
max_multi_message_length = 67 | |
} | |
if mb_len > max_message_length { | |
return uint64(math.Ceil(float64(mb_len) / float64(max_multi_message_length))) | |
} | |
return 1 | |
} | |
func main() { | |
message := os.Args[1] | |
fmt.Printf("Message \"%s\" will be sent as #%d fragments\n", message, calculateSmsFragments(message)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment