Skip to content

Commit 26cf12e

Browse files
authored
Update 772-string-bcs.markdown
1 parent 3ba7f29 commit 26cf12e

File tree

1 file changed

+26
-72
lines changed

1 file changed

+26
-72
lines changed

_build/reference/772-string-bcs.markdown

Lines changed: 26 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,76 +2,30 @@
22

33
> BCS (s)
44
5-
Converts (B)ASIC-style strings to (C)-style (S)trings.
6-
7-
8-
~~~
9-
10-
' Note: BCS allows to save special formatting characters as a regular text (or
11-
' you could directly use C-Style formatting without using BCS at all).
12-
' --- Formatting strings/characters/functions:
13-
' " and \\ are common to SB and C - so to avoid problems use C-Style directly:
14-
Const DQM = "\\x22" ' " character as C-Style string (Double Quotation Mark)
15-
Const BKS = "\\x5C" ' \\ character as C-Style string (Back Slash)
16-
' CHR(0) signals the End-Of-String in C-String (Null-Terminated-String):
17-
Const NTS = "\\0"
18-
' Special formating strings:
19-
Const CSI = Chr(27) + "[" ' Control Sequence Introducer
20-
Const CSI_NORMAL = CSI + "0m" ' Reset all terminal attributes
21-
Const CSI_YELLOW = CSI + "33m" ' Supported foreground colors (30m-37m)
22-
Const CSI_CYAN = CSI + "36m"
23-
Const CSI_WHITE = CSI + "37m"
24-
Const CSI_BOLD = CSI + "1m" ' = Cat(1)
25-
Const CSI_ULINE = CSI + "4m" ' = Cat(2)
26-
' Special formating characters:
27-
Const C0_HT = Chr(9) ' HT - Horizontal Tabulation (tab) (-C- \\t)
28-
Const C0_LF = Chr(10) ' LF - Line Feed (-C- \\n)
29-
Const C0_FF = Chr(12) ' FF - Form Feed (clear screen) (-C- \\f)
30-
Const C0_CR = Chr(13) ' CR - Carriage Return (-C- \\r)
31-
Const C0_NL = C0_CR + C0_LF ' CNL - Cursor Next Line (Windows style for \\n)
32-
Def tb(n) = String(n, C0_HT) ' few tabs (HT)
33-
Def nl(n) = String(n, C0_NL) ' few New lines (CNL)
34-
' Headings text formats:
35-
Const NEW_PAGE = Bcs(CSI_NORMAL + C0_FF)
36-
Def H1(s) = Bcs(nl(2) + tb(5) + CSI_BOLD + CSI_WHITE + s + CSI_NORMAL + nl(1))
37-
Def H2(s) = Bcs(nl(1) + tb(1) + CSI_ULINE + CSI_YELLOW + s + CSI_NORMAL + nl(1))
38-
Def H3(s) = Bcs(nl(1) + tb(2) + CSI_ULINE + CSI_CYAN + s + CSI_NORMAL + nl(1))
39-
' Normal text formats:
40-
Func T2(s) = Bcs(tb(1) + s)
41-
Func T3(s) = Bcs(tb(2) + s)
42-
43-
' --- Start demo:
44-
' Convert some text to C-Style strings, and append to t array:
45-
t << NEW_PAGE
46-
t << H1("Main Title")
47-
t << H2("Sub Title")
48-
t << T2("Some " + DQM + "text" + DQM + " in English,")
49-
t << T2("Some " + BKS + "more text" + BKS + " in English -")
50-
t << T2("And even more text in English.")
51-
t << H3("Sub-Sub Title")
52-
t << T3("And again! text in English;")
53-
t << T3("That's it." + NTS + "This text is hidden from C ... :)")
54-
' Save t array in a Regular text file (as C-Style strings):
55-
Tsave "demo-doc.txt", t
56-
' Load lines from text file into a array:
57-
Tload "demo-doc.txt", a
58-
' Convert C-Style strings back to Basic-Style strings and print them:
59-
For i in a
60-
? Cbs(i)
61-
Next
62-
Pause
63-
' "demo-doc.txt" file will contain these lines:
64-
' ---------------------------------------------
65-
' \\e[0m\\f
66-
' \\r\\n\\r\\n\\t\\t\\t\\t\\t\\e[1m\\e[37mMain Title\\e[0m\\r\\n
67-
' \\r\\n\\t\\e[4m\\e[33mSub Title\\e[0m\\r\\n
68-
' \\tSome \\x22text\\x22 in English,
69-
' \\tSome \\x5Cmore text\\x5C in English -
70-
' \\tAnd even more text in English.
71-
' \\r\\n\\t\\t\\e[4m\\e[36mSub-Sub Title\\e[0m\\r\\n
72-
' \\t\\tAnd again! text in English;
73-
' \\t\\tThat's it.\\0This text is hidden from C ... :)
74-
75-
~~~
76-
5+
Converts (B)ASIC-style strings to (C)-style (S)trings by masking escape codes. Escape codes in the string will not be applied to format the string when printed on the screen. Instead escape codes will be printed as they are.
6+
7+
See CBS to unmasked a c-style string, and see article "Escape codes" for more information about escape codes.
8+
9+
```
10+
' define some escape codes
11+
const NORMAL = "\e[0m" ' = Cat(0)
12+
const BOLD = "\e[1m" ' = Cat(1)
13+
const BOLD_OFF = "\e[21m" ' = Cat(-1)
14+
const BG_CYAN = "\e[46m"
15+
const TB = "\t"
16+
17+
' Create a string with escape codes
18+
BasicStyleString = BOLD + "BOLD" + BOLD_OFF + TB + TB + BG_CYAN + "COLORFULL BG" + NORMAL
19+
CStyleString = bcs(BasicStyleString)
20+
BackConverted_BasicStyleString = cbs(CStyleString)
21+
22+
print "Basic-style string : "; BasicStyleString
23+
print "C-style string : "; CStyleString
24+
print "Back conversion to basic style: "; BackConverted_BasicStyleString
25+
26+
' Output will be (with colors and in bold)
27+
' Basic-style string : BOLD COLORFULL BG
28+
' C-style string : \e[1mBOLD\e[21m\t\t\e[46mCOLORFULL BG\e[0m
29+
' Back conversion to basic style: BOLD COLORFULL BG
30+
```
7731

0 commit comments

Comments
 (0)