udfStrToMorse (2)
str udfStrToMorse (str)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrToMorse (strString) ; Version 2.
If strString == "" Then Return ""
; Chars = 26 upper alpha chars + 10 numerical chars + 23 upper special chars + one space char. Note: "CH" = "----" ; Not handled by this code.
strChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" : "0123456789" : "ÀÅÄÈÉÖÜßÑ.,:;?-_()'=+/@"
strDelim = "|"
strMorseLatin = ".-|-...|-.-.|-..|.|..-.|--.|....|..|.---|-.-|.-..|--|-.|---|.--.|--.-|.-.|...|-|..-|...-|.--|-..-|-.--|--.." ; 26 codes.
strMorseNum = "-----|.----|..---|...--|....-|.....|-....|--...|---..|----." ; 10 codes.
strMorseSpecial = ".--.-|.--.-|.-.-|.-..-|..-..|---.|..--|...--..|--.--|.-.-.-|--..--|---...|-.-.-.|..--..|-....-|..--.-|-.--.|-.--.-|.----.|-...-|.-.-.|-..-.|.--.-." ; 23 codes.
arrMorse = Arrayize (strMorseLatin : strDelim : strMorseNum : strDelim : strMorseSpecial, strDelim) ; 59 codes.
strSpace = " "
strPoint = "."
strHyphen = "-"
strSurrogateSpace = Num2Char (1)
strSurrogatePoint = Num2Char (2)
strSurrogateHyphen = Num2Char (3)
strRemoveChars = strSurrogateSpace : strSurrogatePoint : strSurrogateHyphen : strPoint : strHyphen
strSaveChars = strSurrogateSpace : strSurrogatePoint : strSurrogateHyphen : strChars
strString = StrReplace (strString, strSpace, strSurrogateSpace)   ; Mask all spaces.
strString = StrReplace (strString, strPoint, strSurrogatePoint)   ; Mask all points.
strString = StrReplace (strString, strHyphen, strSurrogateHyphen) ; Mask all hyphens.
strString = StrClean (strString, strSaveChars, "", @FALSE, 2) ; Remove all other unknown chars.
strString = StrUpper (strString)
While @TRUE
   strTemp = StrClean (strString, strRemoveChars, "", @TRUE, 1) ; Remove all Morse chars, create remaining chars list.
   If strTemp == "" Then Break
   strChar = StrSub (strTemp, 1, 1) ; Get next char from remaining chars list.
   strMorse = arrMorse [StrIndex (strChars, strChar, 1, @FWDSCAN) - 1] ; Lookup Morse code.
   strString = StrReplace (strString, strChar, strMorse) ; Replace all same chars with same Morse code.
EndWhile
strString = StrReplace (strString, strSurrogateHyphen, arrMorse [StrIndex (strChars, strHyphen, 1, @FWDSCAN) - 1]) ; Demask and encode all hyphens-
strString = StrReplace (strString, strSurrogatePoint, arrMorse [StrIndex (strChars, strPoint, 1, @FWDSCAN) - 1]) ; Demask and encode all points-
strString = StrReplace (strString, strSurrogateSpace, strSpace) ; Demask all spaces-
Return strString
;..........................................................................................................................................
; This UDF "udfStrToMorse" converts a given string into it's equivalent Morse code.
;
; strCharsLatin = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ; 26 upper alpha chars.
; strCharsNum = "0123456789" ; 10 numerical chars.
; strCharsSpecial = "ÀÅÄÈÉÖÜßÑ.,:;?-_()'=+/@" ; 23 special chars.
; Note: CH = "----" ; not handled by this code.
;
; Detlev Dalitz.20090703.20101223.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

strString = "SOS"
strMorse = udfStrToMorse (strString) ; strMorse = "...---..."
strMsgTitle = strString
strMsgText = strMorse
Message (strMsgTitle, strMsgText)

strString = "- (  [<{Hello}>] World!  ) ? ."
strMorse = udfStrToMorse (strString) ; strMorse = "-....- -.--.  ......-...-..--- .-----.-..-..-..  -.--.- ..--.. .-.-.-"
strMsgTitle = strString
strMsgText = strMorse
Message (strMsgTitle, strMsgText)

Exit