Repository navigation
Expand file tree
/
Copy pathSmartMath_CopyNormalize.bas
More file actions
142 lines (131 loc) · 4.1 KB
/
Copy pathSmartMath_CopyNormalize.bas
File metadata and controls
142 lines (131 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include once "SmartMath_Globals.bi"
private function IndexOfExponentLetterCopy(byref s as String) as Integer
dim n as Integer = Len(s)
dim i as Integer
for i = 1 to n
select case Asc(s, i)
case 69, 101
return i
end select
next i
return 0
end function
private function StripThousandsFromCopyToken(byref t as String) as String
if g_bUseThousandsSeparator = FALSE then return t
dim d as String = g_sThousandsSeparator
dim dlen as Integer = Len(d)
if dlen = 0 then return t
dim r as String = t
dim p as Integer = InStr(1, r, d)
while p > 0
r = Left(r, p - 1) & Mid(r, p + dlen)
p = InStr(1, r, d)
wend
return r
end function
'' Margin uses g_sArrayOutputSeparator & " " between elements; split on that so decimal commas stay intact.
private function SplitCopyArrayInner(byref inner as String, elems() as String) as Integer
erase elems
dim s as String = inner
dim n as Integer = Len(s)
if n = 0 then return 0
dim delim as String = Trim(g_sArrayOutputSeparator) & " "
dim dlen as Integer = Len(delim)
if dlen < 1 then return 0
dim depth as Integer = 0
dim start as Integer = 1
dim partCount as Integer = 0
dim i as Integer = 1
while i <= n
dim ch as String = Mid(s, i, 1)
if ch = "(" then
depth += 1
i += 1
elseif ch = ")" then
if depth > 0 then depth -= 1
i += 1
elseif depth = 0 andalso i + dlen - 1 <= n andalso Mid(s, i, dlen) = delim then
dim seg as String = Trim(Mid(s, start, i - start))
if partCount = 0 then
redim elems(0 to 0)
else
redim preserve elems(0 to partCount)
end if
elems(partCount) = seg
partCount += 1
i += dlen
start = i
else
i += 1
end if
wend
dim segLast as String = Trim(Mid(s, start, n - start + 1))
if partCount = 0 then
redim elems(0 to 0)
else
redim preserve elems(0 to partCount)
end if
elems(partCount) = segLast
return partCount + 1
end function
'' Clipboard: ASCII "." decimals (parser-friendly). Strip g_sThousandsSeparator only when
'' g_bUseThousandsSeparator is true; honor g_sDecimalSeparator; if decimal is "." but a comma
'' remains in the mantissa (locale/display mismatch), normalize those commas too.
private function ElemToCanonicalCopy(byref seg as String) as String
dim t as String = Trim(seg)
if Len(t) = 0 then return t
dim tl as String = LCase(Left(t, 2))
if tl = "0x" orelse tl = "0b" orelse tl = "0o" then return t
t = StripThousandsFromCopyToken(t)
dim ePos as Integer = IndexOfExponentLetterCopy(t)
dim mant as String
dim eTail as String
if ePos > 0 then
mant = Left(t, ePos - 1)
eTail = Mid(t, ePos)
else
mant = t
eTail = ""
end if
dim ds as String = g_sDecimalSeparator
dim dslen as Integer = Len(ds)
if dslen > 0 andalso ds <> "." then
dim p as Integer = InStr(1, mant, ds)
while p > 0
mant = Left(mant, p - 1) & "." & Mid(mant, p + dslen)
p = InStr(1, mant, ds)
wend
end if
if g_sDecimalSeparator = "." then
dim p2 as Integer = InStr(1, mant, ",")
while p2 > 0
mant = Left(mant, p2 - 1) & "." & Mid(mant, p2 + 1)
p2 = InStr(1, mant, ",")
wend
end if
return mant & eTail
end function
function NormalizeCopiedResult(byref sRes as String) as String
if left(sRes, Len(SMARTMATH_RESULT_PREFIX)) = SMARTMATH_RESULT_PREFIX then
dim sOut as String = mid(sRes, Len(SMARTMATH_RESULT_PREFIX) + 1)
sOut = Trim(sOut)
if Len(sOut) >= 2 andalso Left(sOut, 1) = "(" andalso Right(sOut, 1) = ")" then
dim inner as String = Trim(Mid(sOut, 2, Len(sOut) - 2))
dim elems() as String
dim cnt as Integer = SplitCopyArrayInner(inner, elems())
if cnt <= 0 then return "()"
dim acc as String = "("
dim ei as Integer
for ei = 0 to cnt - 1
if ei > 0 then acc &= ", "
acc &= ElemToCanonicalCopy(elems(ei))
next ei
acc &= ")"
return acc
end if
return ElemToCanonicalCopy(sOut)
elseif left(sRes, Len(SMARTMATH_ERROR_PREFIX)) = SMARTMATH_ERROR_PREFIX then
return mid(sRes, Len(SMARTMATH_ERROR_PREFIX) + 1)
end if
return sRes
end function