morse

package module
v1.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2018 License: MIT Imports: 4 Imported by: 1

README

Morse

Documentation Go Report Card

Morse is a simple library for encoding and decoding between morse and text.

Support

This library supports the default morse (as defined by ITU-R M.1677-1) code but custom ones can be used freely using a custom EncodingMap

Tool

You can find a simple tool in the cmd/morse directory This tool can be used for converting to/from morse

$ morse > out.morse
test
this is morse.
^C
$ cat out.morse
- . ... - .-.-- .... .. ...   .. ...   -- --- .-. ... . .-.-.- .-.-
$ morse -D < out.morse
TEST
THIS IS MORSE.

For more uses look use --help

Examples

text := "MORSE IS AWESOME"

//Convert to morse
textInMorse := morse.ToMorse(text)
fmt.Println(textInMorse) //-- --- .-. ... .   .. ...   .- .-- . ... --- -- .

//Back to text
backToText := morse.ToText(textInMorse)
fmt.Println(backToText) //MORSE IS AWESOME

You can see more examples on the godoc documentation

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

Examples

Constants

View Source
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

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

View Source
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

func IgnoreHandler(error) string

IgnoreHandler ignores the error and returns nothing

func PanicHandler

func PanicHandler(err error) string

PanicHandler is a handler that panics when an error occurs

func RuneToMorse

func RuneToMorse(ch rune) string

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

func RuneToText(char string) rune

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

func ToMorse(text string) string

ToMorse converts a text to his morse rrpresentation, it is an alias to DefaultConverter.ToMorse

func ToMorseWriter

func ToMorseWriter(output io.Writer) io.Writer

ToMorseWriter translates all the text written to the returned io.Writer in morse code and writes it in the input io.Writer

func ToText

func ToText(morse string) string

ToText converts a morse string to his textual representation, it is an alias to DefaultConverter.ToText

func ToTextWriter

func ToTextWriter(output io.Writer) io.Writer

ToTextWriter translates all the text written to the returned io.Writer from 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

func (c Converter) CharSeparator() string

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

func (c Converter) ToMorse(text string) string

ToMorse converts a text to his morse representation Lowercase characters are automatically converted to Uppercase

For Example: "Test" -> "- . ... -"

func (Converter) ToMorseWriter

func (c Converter) ToMorseWriter(output io.Writer) io.Writer

ToMorseWriter translate all the text written to the returned io.Writer in morse code and writes it in the input io.Writer

func (Converter) ToText

func (c Converter) ToText(morse string) string

ToText converts a morse string to his textual representation

For Example: "- . ... -" -> "TEST"

func (Converter) ToTextWriter

func (c Converter) ToTextWriter(output io.Writer) io.Writer

ToTextWriter translate all the text written to the returned io.Writer from morse code and writes it in the input io.Writer

type ConverterOption

type ConverterOption func(Converter) Converter

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

type EncodingMap map[rune]string

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

type ErrorHandler func(error) string

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

Directories

Path Synopsis
cmd
morse command
This is a simple tool for converting between Text and morse representation
This is a simple tool for converting between Text and morse representation

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL