• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DomCR / ACadSharp / 19643235357

24 Nov 2025 05:25PM UTC coverage: 78.033% (-0.2%) from 78.204%
19643235357

push

github

web-flow
Merge pull request #890 from DomCR/Issue-250_Text-MText-special-characters

issue 250

7457 of 10365 branches covered (71.94%)

Branch coverage included in aggregate %.

75 of 140 new or added lines in 2 files covered. (53.57%)

54 existing lines in 3 files now uncovered.

27717 of 34711 relevant lines covered (79.85%)

97757.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

49.78
/src/ACadSharp/Text/TextProcessor.cs
1
using CSUtilities.Extensions;
2
using System;
3
using System.Collections.Generic;
4
using System.Text;
5

6
namespace ACadSharp.Text
7
{
8
        public static class TextProcessor
9
        {
10
                public static string Parse(string text, out List<string> groups)
11
                {
8✔
12
                        groups = new List<string>();
8✔
13
                        if (string.IsNullOrEmpty(text))
8!
NEW
14
                        {
×
NEW
15
                                return text;
×
16
                        }
17

18
                        StringBuilder sb = new StringBuilder();
8✔
19

20
                        int index = 0;
8✔
21
                        bool openGroup = false;
8✔
22
                        while (index < text.Length)
335✔
23
                        {
327✔
24
                                char? prev = text.TryGet(index - 1);
327✔
25
                                char? current = text.TryGet(index);
327✔
26
                                char? next = text.TryGet(index + 1);
327✔
27

28
                                if (current == '\\' && next.HasValue)
327!
29
                                {
31✔
30
                                        switch (next)
31!
31
                                        {
32
                                                case '}':
33
                                                case '{':
34
                                                case '\\':
35
                                                        sb.Append(next);
3✔
36
                                                        index += 2; 
3✔
37
                                                        break;
3✔
38
                                                case 'c':
39
                                                case 'C':
40
                                                        processColor(text, index, out index);
7✔
41
                                                        break;
7✔
42
                                                case 'f':
43
                                                case 'F':
44
                                                        processFont(text, index, out index);
9✔
45
                                                        break;
9✔
46
                                                case 'h':
47
                                                case 'H':
48
                                                        processHeight(text, index, out index);
3✔
49
                                                        break;
3✔
50
                                                case 'p':
51
                                                        processJustification(text, index, out index);
2✔
52
                                                        break;
2✔
53
                                                case 'P':
54
                                                case 'n':
55
                                                        sb.Append(Environment.NewLine);
7✔
56
                                                        index += 2;
7✔
57
                                                        break;
7✔
58
                                                default:
NEW
59
                                                        index++;
×
NEW
60
                                                        break;
×
61
                                        }
62
                                }
31✔
63
                                else if (current == '{' && prev != '\\')
296!
64
                                {
10✔
65
                                        openGroup = true;
10✔
66
                                        index++;
10✔
67
                                }
10✔
68
                                else if (current == '}' && prev != '\\')
286!
69
                                {
10✔
70
                                        openGroup = false;
10✔
71
                                        index++;
10✔
72
                                }
10✔
73
                                else
74
                                {
276✔
75
                                        sb.Append(current);
276✔
76
                                        index++;
276✔
77
                                }
276✔
78
                        }
327✔
79

80
                        return sb.ToString();
8✔
81
                }
8✔
82

83

84
                public static string Unescape(string text)
NEW
85
                {
×
NEW
86
                        if (string.IsNullOrEmpty(text))
×
NEW
87
                        {
×
NEW
88
                                return text;
×
89
                        }
90

NEW
91
                        StringBuilder sb = new StringBuilder();
×
92

NEW
93
                        int index = 0;
×
NEW
94
                        bool openGroup = false;
×
NEW
95
                        while (index < text.Length)
×
NEW
96
                        {
×
NEW
97
                                int currIndex = text.IndexOf(@"\", index);
×
NEW
98
                                if (currIndex <= 0)
×
NEW
99
                                {
×
NEW
100
                                        string s = text.Substring(index, text.Length - index);
×
NEW
101
                                        if (openGroup && s.Contains("}"))
×
NEW
102
                                        {
×
NEW
103
                                                s = s.Replace("}", string.Empty);
×
NEW
104
                                                openGroup = false;
×
NEW
105
                                        }
×
NEW
106
                                        sb.Append(s);
×
NEW
107
                                        break;
×
108
                                }
109

NEW
110
                                char prev = text[currIndex - 1];
×
NEW
111
                                char current = text[currIndex];
×
NEW
112
                                char next = text[currIndex + 1];
×
113

NEW
114
                                if (prev == '{')
×
NEW
115
                                {
×
NEW
116
                                        currIndex--;
×
NEW
117
                                        openGroup = true;
×
NEW
118
                                }
×
119

NEW
120
                                if (currIndex > index)
×
NEW
121
                                {
×
NEW
122
                                        string s = text.Substring(index, currIndex - index);
×
NEW
123
                                        if (openGroup && s.Contains("}"))
×
NEW
124
                                        {
×
NEW
125
                                                s = s.Replace("}", string.Empty);
×
NEW
126
                                                openGroup = false;
×
NEW
127
                                        }
×
NEW
128
                                        sb.Append(s);
×
NEW
129
                                }
×
130

131
                                int f;
NEW
132
                                switch (next)
×
133
                                {
134
                                        case 'f':
135
                                        case 'F':
NEW
136
                                                processFont(text, currIndex, out f);
×
NEW
137
                                                currIndex = f;
×
NEW
138
                                                break;
×
139
                                        case 'c':
140
                                        case 'C':
NEW
141
                                                processColor(text, currIndex, out f);
×
NEW
142
                                                currIndex = f;
×
NEW
143
                                                break;
×
144
                                        case 'P':
145
                                        case 'n':
NEW
146
                                                sb.Append(Environment.NewLine);
×
NEW
147
                                                currIndex += 2;
×
NEW
148
                                                break;
×
149
                                        case 'r':
NEW
150
                                                break;
×
151
                                        case '}':
152
                                        case '{':
153
                                        case '\\':
NEW
154
                                                sb.Append(next);
×
NEW
155
                                                break;
×
156
                                }
157

NEW
158
                                index = currIndex;
×
NEW
159
                        }
×
160

NEW
161
                        return sb.ToString();
×
NEW
162
                }
×
163

164
                private static void processFont(string text, int start, out int end)
165
                {
9✔
166
                        //Example:
167
                        //- Font: {\\fCalibri|b0|i0|c0|p34;Calibri\\Fcdm|c0; CDM \\fConsolas|b0|i0|c0|p49;Consolas\\P}
168
                        end = text.IndexOf(';', start);
9✔
169
                        end += 1;
9✔
170
                        var data = text.Substring(start, end - start).Split('|');
9✔
171

172
                        FontData fontData = new FontData
9✔
173
                        {
9✔
174
                                Name = data[0],
9✔
175
                        };
9✔
176
                }
9✔
177

178
                private static void processColor(string text, int start, out int end)
179
                {
7✔
180
                        //Example:
181
                        //- Color text {\\C3;green}, {\\C5;blue}, {\\C1;red}, ByLayer, {\\C0;ByBlock}, {\\C21;\\c5872631;TrueColor}
182
                        end = text.IndexOf(';', start);
7✔
183
                        end += 1;
7✔
184
                }
7✔
185

186
                private static void processHeight(string text, int start, out int end)
187
                {
3✔
188
                        //Example:
189
                        //- {\\H2x;Double height \\H0.875x;height is: 0.35\\H1.14286x;\\P}
190
                        end = text.IndexOf(';', start);
3✔
191
                        end += 1;
3✔
192
                }
3✔
193

194
                private static void processJustification(string text, int start, out int end)
195
                {
2✔
196
                        //Example:
197
                        //\\pxqc;Text in the center\\P\\pq*;Hello this is an mText\\P
198
                        end = text.IndexOf(';', start);
2✔
199
                        end += 1;
2✔
200
                }
2✔
201
        }
202

203
        internal struct FontData
204
        {
205
                public string Name { get; set; }
9✔
206
        }
207
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc