|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +namespace System.Management.Automation |
| 5 | +{ |
| 6 | + public partial class CommandCompletion |
| 7 | + { |
| 8 | + /// <summary> |
| 9 | + /// LastWordFinder implements the algorithm we use to search for the last word in a line of input taken from the console. |
| 10 | + /// This class exists for legacy purposes only - V3 and forward uses a slightly different interface. |
| 11 | + /// </summary> |
| 12 | + private sealed class LastWordFinder |
| 13 | + { |
| 14 | + internal static string FindLastWord(string sentence, out int replacementIndexOut, out char closingQuote) |
| 15 | + { |
| 16 | + return (new LastWordFinder(sentence)).FindLastWord(out replacementIndexOut, out closingQuote); |
| 17 | + } |
| 18 | + |
| 19 | + private LastWordFinder(string sentence) |
| 20 | + { |
| 21 | + _replacementIndex = 0; |
| 22 | + Diagnostics.Assert(sentence != null, "need to provide an instance"); |
| 23 | + _sentence = sentence; |
| 24 | + } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Locates the last "word" in a string of text. A word is a conguous sequence of characters that are not |
| 28 | + /// whitespace, or a contiguous set grouped by single or double quotes. Can be called by at most 1 thread at a time |
| 29 | + /// per LastWordFinder instance. |
| 30 | + /// </summary> |
| 31 | + /// <param name="replacementIndexOut"> |
| 32 | + /// Receives the character index (from the front of the string) of the starting point of the located word, or 0 if |
| 33 | + /// the word starts at the beginning of the sentence. |
| 34 | + /// </param> |
| 35 | + /// <param name="closingQuote"> |
| 36 | + /// Receives the quote character that would be needed to end the sentence with a balanced pair of quotes. For |
| 37 | + /// instance, if sentence is "foo then " is returned, if sentence if "foo" then nothing is returned, if sentence is |
| 38 | + /// 'foo then ' is returned, if sentence is 'foo' then nothing is returned. |
| 39 | + /// </param> |
| 40 | + /// <returns>The last word located, or the empty string if no word could be found.</returns> |
| 41 | + private string FindLastWord(out int replacementIndexOut, out char closingQuote) |
| 42 | + { |
| 43 | + bool inSingleQuote = false; |
| 44 | + bool inDoubleQuote = false; |
| 45 | + |
| 46 | + ReplacementIndex = 0; |
| 47 | + |
| 48 | + for (_sentenceIndex = 0; _sentenceIndex < _sentence.Length; ++_sentenceIndex) |
| 49 | + { |
| 50 | + Diagnostics.Assert(!(inSingleQuote && inDoubleQuote), |
| 51 | + "Can't be in both single and double quotes"); |
| 52 | + |
| 53 | + char c = _sentence[_sentenceIndex]; |
| 54 | + |
| 55 | + // there are 3 possibilities: |
| 56 | + // 1) a new sequence is starting, |
| 57 | + // 2) a sequence is ending, or |
| 58 | + // 3) a sequence is due to end on the next matching quote, end-of-sentence, or whitespace |
| 59 | + |
| 60 | + if (c == '\'') |
| 61 | + { |
| 62 | + HandleQuote(ref inSingleQuote, ref inDoubleQuote, c); |
| 63 | + } |
| 64 | + else if (c == '"') |
| 65 | + { |
| 66 | + HandleQuote(ref inDoubleQuote, ref inSingleQuote, c); |
| 67 | + } |
| 68 | + else if (c == '`') |
| 69 | + { |
| 70 | + Consume(c); |
| 71 | + if (++_sentenceIndex < _sentence.Length) |
| 72 | + { |
| 73 | + Consume(_sentence[_sentenceIndex]); |
| 74 | + } |
| 75 | + } |
| 76 | + else if (IsWhitespace(c)) |
| 77 | + { |
| 78 | + if (_sequenceDueToEnd) |
| 79 | + { |
| 80 | + // we skipped a quote earlier, now end that sequence |
| 81 | + |
| 82 | + _sequenceDueToEnd = false; |
| 83 | + if (inSingleQuote) |
| 84 | + { |
| 85 | + inSingleQuote = false; |
| 86 | + } |
| 87 | + |
| 88 | + if (inDoubleQuote) |
| 89 | + { |
| 90 | + inDoubleQuote = false; |
| 91 | + } |
| 92 | + |
| 93 | + ReplacementIndex = _sentenceIndex + 1; |
| 94 | + } |
| 95 | + else if (inSingleQuote || inDoubleQuote) |
| 96 | + { |
| 97 | + // a sequence is started and we're in quotes |
| 98 | + |
| 99 | + Consume(c); |
| 100 | + } |
| 101 | + else |
| 102 | + { |
| 103 | + // no sequence is started, so ignore c |
| 104 | + |
| 105 | + ReplacementIndex = _sentenceIndex + 1; |
| 106 | + } |
| 107 | + } |
| 108 | + else |
| 109 | + { |
| 110 | + // a sequence is started and we're in it |
| 111 | + |
| 112 | + Consume(c); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + string result = new string(_wordBuffer, 0, _wordBufferIndex); |
| 117 | + |
| 118 | + closingQuote = inSingleQuote ? '\'' : inDoubleQuote ? '"' : '\0'; |
| 119 | + replacementIndexOut = ReplacementIndex; |
| 120 | + return result; |
| 121 | + } |
| 122 | + |
| 123 | + private void HandleQuote(ref bool inQuote, ref bool inOppositeQuote, char c) |
| 124 | + { |
| 125 | + if (inOppositeQuote) |
| 126 | + { |
| 127 | + // a sequence is started, and we're in it. |
| 128 | + Consume(c); |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + if (inQuote) |
| 133 | + { |
| 134 | + if (_sequenceDueToEnd) |
| 135 | + { |
| 136 | + // I've ended a sequence and am starting another; don't consume c, update replacementIndex |
| 137 | + ReplacementIndex = _sentenceIndex + 1; |
| 138 | + } |
| 139 | + |
| 140 | + _sequenceDueToEnd = !_sequenceDueToEnd; |
| 141 | + } |
| 142 | + else |
| 143 | + { |
| 144 | + // I'm starting a sequence; don't consume c, update replacementIndex |
| 145 | + inQuote = true; |
| 146 | + ReplacementIndex = _sentenceIndex; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + private void Consume(char c) |
| 151 | + { |
| 152 | + Diagnostics.Assert(_wordBuffer != null, "wordBuffer is not initialized"); |
| 153 | + Diagnostics.Assert(_wordBufferIndex < _wordBuffer.Length, "wordBufferIndex is out of range"); |
| 154 | + |
| 155 | + _wordBuffer[_wordBufferIndex++] = c; |
| 156 | + } |
| 157 | + |
| 158 | + private int ReplacementIndex |
| 159 | + { |
| 160 | + get |
| 161 | + { |
| 162 | + return _replacementIndex; |
| 163 | + } |
| 164 | + |
| 165 | + set |
| 166 | + { |
| 167 | + Diagnostics.Assert(value >= 0 && value < _sentence.Length + 1, "value out of range"); |
| 168 | + |
| 169 | + // when we set the replacement index, that means we're also resetting our word buffer. we know wordBuffer |
| 170 | + // will never be longer than sentence. |
| 171 | + |
| 172 | + _wordBuffer = new char[_sentence.Length]; |
| 173 | + _wordBufferIndex = 0; |
| 174 | + _replacementIndex = value; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + private static bool IsWhitespace(char c) |
| 179 | + { |
| 180 | + return (c == ' ') || (c == '\x0009'); |
| 181 | + } |
| 182 | + |
| 183 | + private readonly string _sentence; |
| 184 | + private char[] _wordBuffer; |
| 185 | + private int _wordBufferIndex; |
| 186 | + private int _replacementIndex; |
| 187 | + private int _sentenceIndex; |
| 188 | + private bool _sequenceDueToEnd; |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments