Xlate$ Function

Purpose

Replaces all characters of a string expression with values from a table.

Syntax

$ = Xlate[$](a$, mi())

$ = Xlate[$](a$, m$())

$ = Xlate[$](a$, addr)

a$:sexp
mi():integer array variable (%,&,|)
m$():string array variable
addr:iexp

Description

Xlate$(a$, m()) converts each character in the string expression a$ using the user-created table in m(). The array can be of any integer type (Byte, Short, Card or Integer/Long).

The character of the string is replaced with value in the array at the index which corresponds with the ASCII code of the character. For instance, when the string a$ contains the character 'A', then it is replaced with the character value at index = 65 in the array, because the ASCII code of 'A' is 65.

XLate() can also take an address of a byte array of 256 characters. This provides a way to use a string as a replacement table. See the example.

GFA-BASIC 32 extends the XLate function by using a string array rather than an integer array which only contains one character value. By using a 256 elements string array each character in a$ can be replaced by an entire string, instead of only one character. See example 2.

Example

Local a$, b$ , i%

For i% = 0 To 255 ' Create a table

b$ = Chr$(i%)

If b$ = Upper$(b$)

b$ = Lower$(b$)

Else

b$ = Upper$(b$)

EndIf

a$ = a$ + b$

Next i%

Message XLate$("Hallo World abcABCäöüÄÖÜ", V:a$)

prints: hALLO wORLD ABCabcÄÖÜäöü

Local a$, b$

InitEscape

Local i%, j%, t#

t = Timer

For i = 1 To 100

b = XLate(String(500, "This is a test äöüÄÖÜ"), htmlEscape())

Next

Print Timer - t, b

Do

Sleep

Loop Until Me Is Nothing

 

Sub InitEscape

Local out$, i%, c%

Global Dim htmlEscape$(0 .. 255)

For c = 0 To 255

Switch c

Case ">" : out$ = ">"

Case "<" : out$ = "<"

Case "&" : out$ = "&amp;"

Case "ä" : out$ = "&auml;"

Case "Ä" : out$ = "&Auml;"

Case "ö" : out$ = "&ouml;"

Case "Ö" : out$ = "&Ouml;"

Case "ü" : out$ = "&uuml;"

Case "Ü" : out$ = "&Uuml;"

Case "ß" : out$ = "&szlig;"

Case 0 To 31, 128 To :

out$ = "&#" & Dec$(c) & ";"

Default : out$ = Chr(c)

EndSelect

htmlEscape(c) = out

Next

End Sub

Remarks

Xlate$(a$, m%()) corresponds to

For i% = 1 To Len(a$)

Mid$(a$, i%) = Chr(m%(Asc(Mid$(a$, i%, 1))))

Next i%

See Also

Upper$(), Lower$(), UCase$(), LCase$()

{Created by Sjouke Hamstra; Last updated: 25/10/2014 by James Gaite}