diff --git a/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexParser.cs b/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexParser.cs
index 8885ae485f4957..b374186172159e 100644
--- a/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexParser.cs
+++ b/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexParser.cs
@@ -1628,7 +1628,7 @@ private char ScanControl()
}
/// Returns true for options allowed only at the top level
- private bool IsOnlyTopOption(RegexOptions options) =>
+ private readonly bool IsOnlyTopOption(RegexOptions options) =>
options == RegexOptions.RightToLeft ||
options == RegexOptions.CultureInvariant ||
options == RegexOptions.ECMAScript;
@@ -1748,7 +1748,7 @@ private string ParseProperty()
}
/// Returns ReNode type for zero-length assertions with a \ code.
- private int TypeFromCode(char ch) =>
+ private readonly int TypeFromCode(char ch) =>
ch switch
{
'b' => UseOptionE() ? RegexNode.ECMABoundary : RegexNode.Boundary,
@@ -2021,10 +2021,10 @@ private void AssignNameSlots()
}
/// Looks up the slot number for a given name.
- private int CaptureSlotFromName(string capname) => (int)_capnames![capname]!;
+ private readonly int CaptureSlotFromName(string capname) => (int)_capnames![capname]!;
/// True if the capture slot was noted
- private bool IsCaptureSlot(int i)
+ private readonly bool IsCaptureSlot(int i)
{
if (_caps != null)
{
@@ -2035,25 +2035,25 @@ private bool IsCaptureSlot(int i)
}
/// Looks up the slot number for a given name
- private bool IsCaptureName(string capname) => _capnames != null && _capnames.ContainsKey(capname);
+ private readonly bool IsCaptureName(string capname) => _capnames != null && _capnames.ContainsKey(capname);
/// True if N option disabling '(' autocapture is on.
- private bool UseOptionN() => (_options & RegexOptions.ExplicitCapture) != 0;
+ private readonly bool UseOptionN() => (_options & RegexOptions.ExplicitCapture) != 0;
/// True if I option enabling case-insensitivity is on.
- private bool UseOptionI() => (_options & RegexOptions.IgnoreCase) != 0;
+ private readonly bool UseOptionI() => (_options & RegexOptions.IgnoreCase) != 0;
/// True if M option altering meaning of $ and ^ is on.
- private bool UseOptionM() => (_options & RegexOptions.Multiline) != 0;
+ private readonly bool UseOptionM() => (_options & RegexOptions.Multiline) != 0;
/// True if S option altering meaning of . is on.
- private bool UseOptionS() => (_options & RegexOptions.Singleline) != 0;
+ private readonly bool UseOptionS() => (_options & RegexOptions.Singleline) != 0;
/// True if X option enabling whitespace/comment mode is on.
- private bool UseOptionX() => (_options & RegexOptions.IgnorePatternWhitespace) != 0;
+ private readonly bool UseOptionX() => (_options & RegexOptions.IgnorePatternWhitespace) != 0;
/// True if E option enabling ECMAScript behavior is on.
- private bool UseOptionE() => (_options & RegexOptions.ECMAScript) != 0;
+ private readonly bool UseOptionE() => (_options & RegexOptions.ECMAScript) != 0;
private const byte Q = 5; // quantifier
private const byte S = 4; // ordinary stopper
@@ -2081,7 +2081,7 @@ private bool IsCaptureSlot(int i)
/// Returns true for those characters that begin a quantifier.
private static bool IsQuantifier(char ch) => ch <= '{' && Category[ch] >= Q;
- private bool IsTrueQuantifier()
+ private readonly bool IsTrueQuantifier()
{
Debug.Assert(CharsRight() > 0, "The current reading position must not be at the end of the pattern");
@@ -2185,7 +2185,7 @@ private void PopGroup()
}
/// True if the group stack is empty.
- private bool EmptyStack() => _stack == null;
+ private readonly bool EmptyStack() => _stack == null;
/// Start a new round for the parser state (in response to an open paren or string start)
private void StartGroup(RegexNode openGroup)
@@ -2229,7 +2229,7 @@ private void AddConcatenate(bool lazy, int min, int max)
}
/// Returns the current unit
- private RegexNode? Unit() => _unit;
+ private readonly RegexNode? Unit() => _unit;
/// Sets the current unit to a single char node
private void AddUnitOne(char ch)
@@ -2299,17 +2299,17 @@ private void AddGroup()
private void PopOptions() => _options = (RegexOptions)_optionsStack.Pop();
/// True if options stack is empty.
- private bool EmptyOptionsStack() => _optionsStack.Length == 0;
+ private readonly bool EmptyOptionsStack() => _optionsStack.Length == 0;
/// Pops the options stack, but keeps the current options unchanged.
private void PopKeepOptions() => _optionsStack.Length--;
/// Fills in a RegexParseException
- private RegexParseException MakeException(RegexParseError error, string message) =>
+ private readonly RegexParseException MakeException(RegexParseError error, string message) =>
new RegexParseException(error, _currentPos, SR.Format(SR.MakeException, _pattern, _currentPos, message));
/// Returns the current parsing position.
- private int Textpos() => _currentPos;
+ private readonly int Textpos() => _currentPos;
/// Zaps to a specific parsing position.
private void Textto(int pos) => _currentPos = pos;
@@ -2326,15 +2326,15 @@ private RegexParseException MakeException(RegexParseError error, string message)
private void MoveLeft() => --_currentPos;
/// Returns the char left of the current parsing position.
- private char CharAt(int i) => _pattern[i];
+ private readonly char CharAt(int i) => _pattern[i];
/// Returns the char right of the current parsing position.
- private char RightChar() => _pattern[_currentPos];
+ private readonly char RightChar() => _pattern[_currentPos];
/// Returns the char i chars right of the current parsing position.
- private char RightChar(int i) => _pattern[_currentPos + i];
+ private readonly char RightChar(int i) => _pattern[_currentPos + i];
/// Number of characters to the right of the current parsing position.
- private int CharsRight() => _pattern.Length - _currentPos;
+ private readonly int CharsRight() => _pattern.Length - _currentPos;
}
}
diff --git a/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexPrefixAnalyzer.cs b/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexPrefixAnalyzer.cs
index 2f8d5d15efcebc..c56a2f409074df 100644
--- a/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexPrefixAnalyzer.cs
+++ b/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexPrefixAnalyzer.cs
@@ -381,7 +381,7 @@ public static string AnchorDescription(int anchors)
///
private void PushInt(int i) => _intStack.Append(i);
- private bool IntIsEmpty() => _intStack.Length == 0;
+ private readonly bool IntIsEmpty() => _intStack.Length == 0;
private int PopInt() => _intStack.Pop();
@@ -390,7 +390,7 @@ public static string AnchorDescription(int anchors)
///
private void PushFC(RegexFC fc) => _fcStack.Add(fc);
- private bool FCIsEmpty() => _fcStack.Count == 0;
+ private readonly bool FCIsEmpty() => _fcStack.Count == 0;
private RegexFC PopFC()
{
@@ -399,7 +399,7 @@ private RegexFC PopFC()
return item;
}
- private RegexFC TopFC() => _fcStack[_fcStack.Count - 1];
+ private readonly RegexFC TopFC() => _fcStack[_fcStack.Count - 1];
///
/// Return rented buffers.
diff --git a/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexWriter.cs b/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexWriter.cs
index 2154947cfaa8d5..950e2240f7fa71 100644
--- a/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexWriter.cs
+++ b/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexWriter.cs
@@ -252,7 +252,7 @@ private int StringCode(string str)
/// for an array of capture slots. Instead of doing the hash
/// at match time, it's done at compile time, here.
///
- private int MapCapnum(int capnum) =>
+ private readonly int MapCapnum(int capnum) =>
capnum == -1 ? -1 :
_caps != null ? (int)_caps[capnum]! :
capnum;
diff --git a/src/libraries/System.Text.RegularExpressions/src/System/Text/SegmentStringBuilder.cs b/src/libraries/System.Text.RegularExpressions/src/System/Text/SegmentStringBuilder.cs
index eb19c8574858c9..59efe8c0f4c279 100644
--- a/src/libraries/System.Text.RegularExpressions/src/System/Text/SegmentStringBuilder.cs
+++ b/src/libraries/System.Text.RegularExpressions/src/System/Text/SegmentStringBuilder.cs
@@ -21,7 +21,7 @@ internal struct SegmentStringBuilder
public static SegmentStringBuilder Create() => new SegmentStringBuilder() { _array = Array.Empty>() };
/// Gets the number of segments added to the builder.
- public int Count => _count;
+ public readonly int Count => _count;
/// Adds a segment to the builder.
/// The segment.
@@ -60,7 +60,7 @@ private void GrowAndAdd(ReadOnlyMemory segment)
/// Gets a span of all segments in the builder.
///
- public Span> AsSpan() => new Span>(_array, 0, _count);
+ public readonly Span> AsSpan() => new Span>(_array, 0, _count);
/// Creates a string from all the segments in the builder and then disposes of the builder.
public override string ToString()