Debian Codegraph @

external/nuget-buildtasks/src/Microsoft.NuGet.Build.Tasks/Preprocessor.cs

raw symbols SCIP
1 // Copyright (c) .NET Foundation. All rights reserved.
2 // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
4 using System;
5 using System.Collections.Generic;
6 using System.Diagnostics;
7 using System.Globalization;
8 using System.IO;
9 using System.Linq;
10 using System.Text;
11 using System.Threading.Tasks;
12
13 namespace Microsoft.NuGet.Build.Tasks
14 {
15 public static class Preprocessor
16 {
17 private const char PreprocessorDelimiter = '$';
18
19 public static void Preprocess(TextReader reader, TextWriter writer, IReadOnlyDictionary<string, string> values)
20 {
21 // $$ is the escape for a $, which we can just replicate in the algorithm by giving the empty replacement the known value
22 var maximumKeyLength = values.Keys.Max<string, int?>(k => k.Length).GetValueOrDefault(0);
23 char[] buffer = new char[Math.Max(4096, maximumKeyLength + 2)];
24
25 int charsInBuffer = reader.ReadBlock(buffer, 0, buffer.Length);
26
27 while (charsInBuffer != 0)
28 {
29 int indexOfDelimiter = Array.IndexOf(buffer, PreprocessorDelimiter, 0, charsInBuffer);
30
31 if (indexOfDelimiter == -1)
32 {
33 // If the buffer doesn't contain any delimiters, then we can immediately write it out and move on
34 writer.Write(buffer, 0, charsInBuffer);
35 charsInBuffer = reader.ReadBlock(buffer, 0, buffer.Length);
36 }
37 else
38 {
39 // Write whatever is before the $ and advance up to the $
40 writer.Write(buffer, 0, indexOfDelimiter);
41 Advance(reader, buffer, ref charsInBuffer, charsToAdvance: indexOfDelimiter);
42
43 // Let's read in the token name
44 var token = new StringBuilder();
45 int position = 1;
46
47 while (true)
48 {
49 if (position == buffer.Length)
50 {
51 Advance(reader, buffer, ref charsInBuffer, buffer.Length);
52 position = 0;
53 }
54
55 // If at end, we'll want to fall through to the last case
56 var c = position < charsInBuffer ? buffer[position] : '\0';
57
58 if (c == PreprocessorDelimiter)
59 {
60 position++;
61
62 // Is it the escape case?
63 string value;
64 if (token.Length == 0)
65 {
66 writer.Write(PreprocessorDelimiter);
67 }
68 else if (values.TryGetValue(token.ToString(), out value))
69 {
70 writer.Write(value);
71 }
72 else
73 {
74 throw new ExceptionFromResource(nameof(Strings.UnspecifiedToken), PreprocessorDelimiter + token.ToString() + PreprocessorDelimiter);
75 }
76
77 break;
78 }
79 else if (IsTokenCharacter(c))
80 {
81 token.Append(c);
82 position++;
83 }
84 else
85 {
86 // The token ended prematurely, so we just treat it verbatim and write it out
87 writer.Write(PreprocessorDelimiter);
88 writer.Write(token);
89
90 break;
91 }
92 }
93
94 // Advance to the next position to start all over
95 Advance(reader, buffer, ref charsInBuffer, position);
96 }
97 }
98 }
99
100 private static bool IsTokenCharacter(char c)
101 {
102 var category = CharUnicodeInfo.GetUnicodeCategory(c);
103 return category == UnicodeCategory.LowercaseLetter ||
104 category == UnicodeCategory.UppercaseLetter ||
105 category == UnicodeCategory.TitlecaseLetter ||
106 category == UnicodeCategory.OtherLetter ||
107 category == UnicodeCategory.ModifierLetter ||
108 category == UnicodeCategory.DecimalDigitNumber ||
109 category == UnicodeCategory.ConnectorPunctuation;
110
111 }
112
113 private static void Advance(TextReader reader, char[] buffer, ref int charsInBuffer, int charsToAdvance)
114 {
115 Debug.Assert(charsToAdvance <= charsInBuffer);
116
117 // Move the remaining characters in the buffer forward
118 Array.Copy(sourceArray: buffer, sourceIndex: charsToAdvance, destinationArray: buffer, destinationIndex: 0, length: charsInBuffer - charsToAdvance);
119 charsInBuffer -= charsToAdvance;
120 charsInBuffer += reader.ReadBlock(buffer, charsInBuffer, buffer.Length - charsInBuffer);
121 }
122 }
123 }