Documentation
¶
Overview ¶
Package morse is a simple library for converting text from/to morse
It handles Morse Code as defined by the in ITU-R M.1677-1 https://www.itu.int/dms_pubrec/itu-r/rec/m/R-REC-M.1677-1-200910-I!!PDF-E.pdf
Example ¶
package main
import (
"fmt"
"github.com/gSpera/morse"
)
func main() {
text := "MORSE IS AWESOME"
//Convert to morse
textInMorse := morse.ToMorse(text)
fmt.Println(textInMorse)
//Back to text
backToText := morse.ToText(textInMorse)
fmt.Println(backToText)
}
Output: -- --- .-. ... . .. ... .- .-- . ... --- -- . MORSE IS AWESOME
Index ¶
- Constants
- Variables
- func IgnoreHandler(error) string
- func PanicHandler(err error) string
- func RuneToMorse(ch rune) string
- func RuneToText(char string) rune
- func ToMorse(text string) string
- func ToMorseWriter(output io.Writer) io.Writer
- func ToText(morse string) string
- func ToTextWriter(output io.Writer) io.Writer
- type Converter
- func (c Converter) CharSeparator() string
- func (c Converter) EncodingMap() EncodingMap
- func (c Converter) ToMorse(text string) string
- func (c Converter) ToMorseWriter(output io.Writer) io.Writer
- func (c Converter) ToText(morse string) string
- func (c Converter) ToTextWriter(output io.Writer) io.Writer
- type ConverterOption
- func WithCharSeparator(charSeparator string) ConverterOption
- func WithHandler(handler ErrorHandler) ConverterOption
- func WithLowercaseHandling(lowercaseHandling bool) ConverterOption
- func WithTrailingSeparator(trailingSpace bool) ConverterOption
- func WithWordSeparator(wordSeparator string) ConverterOption
- type EncodingMap
- type ErrNoEncoding
- type ErrorHandler
Examples ¶
Constants ¶
const ( A = ".-" B = "-..." C = "-.-." D = "-.." E = "." AccentedE = "..-.." F = "..-." G = "--." H = "...." I = ".." J = ".---" K = "-.-" L = ".-.." M = "--" N = "-." O = "---" P = ".--." Q = "--.-" R = ".-." S = "..." T = "-" U = "..-" V = "...-" W = ".--" X = "-..-" Y = "-.--" Z = "--.." One = ".----" Two = "..---" Three = "...--" Four = "....-" Five = "....." Six = "-...." Seven = "--..." Eight = "---.." Nine = "----." Zero = "-----" Period = ".-.-.-" //. Comma = "--..--" //, Colon = "---..." //: QuestionMark = "..--.." //? Apostrophe = ".----." //' Hyphen = "-....-" //- Division = "-..-." /// LeftBracket = "-.--." //( RightBracket = "-.--.-" //) IvertedComma = ".-..-." //“ ” DoubleHyphen = "-...-" //= Cross = ".-.-." //+ CommercialAt = ".--.-." //@ Understood = "...-." Error = "........" InvitationToTransmit = "-.-" Wait = ".-..." EndOfWork = "...-.-" StartingSignal = "-.-.-" Space = " " )
Morse letters and figures definitions
Variables ¶
var DefaultConverter = NewConverter( DefaultMorse, WithCharSeparator(" "), WithWordSeparator(" "), WithLowercaseHandling(true), WithHandler(IgnoreHandler), WithTrailingSeparator(false), )
DefaultConverter is the default converter, it uses the exported morse set and has an IgnoreHandler, the separation character is a space Lowercase letter are encoded as upper ones. DefaultConverter uses explicitly IgnoreHandler and adds the trailing separator
var DefaultMorse = EncodingMap{ 'A': A, 'B': B, 'C': C, 'D': D, 'E': E, 'F': F, 'G': G, 'H': H, 'I': I, 'J': J, 'K': K, 'L': L, 'M': M, 'N': N, 'O': O, 'P': P, 'Q': Q, 'R': R, 'S': S, 'T': T, 'U': U, 'V': V, 'W': W, 'X': X, 'Y': Y, 'Z': Z, '1': ".----", '2': "..---", '3': "...--", '4': "....-", '5': ".....", '6': "-....", '7': "--...", '8': "---..", '9': "----.", '0': "-----", '.': ".-.-.-", ',': "--..--", ':': "---...", '?': "..--..", '\'': ".----.", '-': "-....-", '/': "-..-.", '(': "-.--.", ')': "-.--.-", '“': ".-..-.", '=': "-...-", '+': ".-.-.", '@': ".--.-.", ' ': Space, }
DefaultMorse is the default map used to convert between morse and text The map contians all the standard codes defined as costants but doesn't include commands like Understood and Error This map may remain constant.
Functions ¶
func IgnoreHandler ¶
IgnoreHandler ignores the error and returns nothing
func PanicHandler ¶
PanicHandler is a handler that panics when an error occurs
func RuneToMorse ¶
RuneToMorse return the morse representation of the rune If the rune is not a recognized morse character RuneToMorse will return an empty string Lowercase runes are converted to uppercase
For Example: 'T' -> "-"
Example ¶
package main
import (
"fmt"
"github.com/gSpera/morse"
)
func main() {
ch := 'G'
str := morse.RuneToMorse(ch)
fmt.Printf("The letter %c converts to: %s", ch, str)
}
Output: The letter G converts to: --.
func RuneToText ¶
RuneToText return the character represented by the input string If the string is not recognized as a morse sequence RuneToText will return a null rune
For Example: "-" -> 'T'
Example ¶
package main
import (
"fmt"
"github.com/gSpera/morse"
)
func main() {
str := "--."
ch := morse.RuneToText(str)
fmt.Printf("The morse code %s converts to: %c", str, ch)
}
Output: The morse code --. converts to: G
func ToMorse ¶
ToMorse converts a text to his morse rrpresentation, it is an alias to DefaultConverter.ToMorse
func ToMorseWriter ¶
ToMorseWriter translates all the text written to the returned io.Writer in morse code and writes it in the input io.Writer
Types ¶
type Converter ¶
type Converter struct {
Handling ErrorHandler
// contains filtered or unexported fields
}
Converter is a Morse from/to Text converter, it handles the conversion and error handling
func NewConverter ¶
func NewConverter(convertingMap EncodingMap, options ...ConverterOption) Converter
NewConverter creates a new converter with the specified configuration convertingMap is an EncodingMap, it contains how the characters will be translated, usually this is set to DefaultMorse but a custom one can be used. A nil convertingMap will panic.
func (Converter) CharSeparator ¶
CharSeparator returns the charSeparator of the converter
func (Converter) EncodingMap ¶
func (c Converter) EncodingMap() EncodingMap
EncodingMap returns a copy of the EncodingMap inside the Converter, modifying the returned map will not change the internal one
func (Converter) ToMorse ¶
ToMorse converts a text to his morse representation Lowercase characters are automatically converted to Uppercase
For Example: "Test" -> "- . ... -"
func (Converter) ToMorseWriter ¶
ToMorseWriter translate all the text written to the returned io.Writer in morse code and writes it in the input io.Writer
type ConverterOption ¶
ConverterOption is a function that modifies a Converter The main use of ConvertOption is inside NewConverter
func WithCharSeparator ¶
func WithCharSeparator(charSeparator string) ConverterOption
WithCharSeparator sets the Character Separator The CharSeparator is the character used to separate two characters inside a Word
func WithHandler ¶
func WithHandler(handler ErrorHandler) ConverterOption
WithHandler sets the handler for the Converter
func WithLowercaseHandling ¶
func WithLowercaseHandling(lowercaseHandling bool) ConverterOption
WithLowercaseHandling sets if the Converter may convert to uppercase before checking inside the EncodingMap
func WithTrailingSeparator ¶
func WithTrailingSeparator(trailingSpace bool) ConverterOption
WithTrailingSeparator sets if the Converter may trail the charSeparator
func WithWordSeparator ¶
func WithWordSeparator(wordSeparator string) ConverterOption
WithWordSeparator sets the Word Separator The Word Separator is used to separate two words, usually this is the Character Separator, a Space and another Character Separator
type EncodingMap ¶
EncodingMap contains the definitions for converting between two encoding It converts from a text rune (for example 'A') to its morse representation (for example ".-")
type ErrNoEncoding ¶
type ErrNoEncoding struct{ Text string }
ErrNoEncoding is the error used when there is no representation Its primary use is inside Handlers
func (ErrNoEncoding) Error ¶
func (e ErrNoEncoding) Error() string
Error implements the error interface
type ErrorHandler ¶
ErrorHandler is a function used by Converter when it encounters an unknown character Returns the text to insert at the place of the unknown character This may not(but can if necessary) corrupt the output inserting invalid morse character