/// <summary> /// Step through a Range of Strings. /// </summary> /// <remarks> /// This method requires step to be a Fixnum. /// It uses a hybrid string comparison to prevent infinite loops and calls String#succ to get each item in the range. /// </remarks> private static object StepString(EachStorage /*!*/ storage, BlockParam /*!*/ block, Range /*!*/ self, MutableString begin, MutableString end, int step) { Assert.NotNull(storage, block, self); CheckStep(step); object result; MutableString item = begin; int comp; while ((comp = Protocols.Compare(storage, item, end)) < 0) { if (block.Yield(item.Clone(), out result)) { return(result); } if (ReferenceEquals(item, begin)) { item = item.Clone(); } // TODO: this can be optimized for (int i = 0; i < step; i++) { MutableStringOps.SuccInPlace(item); } if (item.Length > end.Length) { return(self); } } if (comp == 0 && !self.ExcludeEnd) { if (block.Yield(item.Clone(), out result)) { return(result); } } return(self); }
internal static MatchData Create(Match /*!*/ match, MutableString /*!*/ input, bool freezeInput, string /*!*/ encodedInput) { if (!match.Success) { return(null); } if (freezeInput) { input = input.Clone().Freeze(); } return(new MatchData(match, input)); }
/// <summary> /// Step through a Range of Strings. /// </summary> /// <remarks> /// This method requires step to be a Fixnum. /// It uses a hybrid string comparison to prevent infinite loops and calls String#succ to get each item in the range. /// </remarks> private static IEnumerable <MutableString> EachStepString(ComparisonStorage /*!*/ storage, Range /*!*/ self, int step) { Assert.NotNull(storage, self); CheckStep(step); var begin = (MutableString)self.Begin; var end = (MutableString)self.End; MutableString item = begin; int comp; while ((comp = Protocols.Compare(storage, item, end)) < 0) { yield return(item.Clone()); if (ReferenceEquals(item, begin)) { item = item.Clone(); } // TODO: this can be optimized for (int i = 0; i < step; i++) { MutableStringOps.SuccInPlace(item); } if (item.Length > end.Length) { yield break; } } if (comp == 0 && !self.ExcludeEnd) { yield return(item.Clone()); } }
public static RubyDir /*!*/ Reinitialize(RubyDir /*!*/ self, [NotNull] MutableString /*!*/ dirname) { self.Close(); string strName = self.ImmediateClass.Context.DecodePath(dirname); try { self._rawEntries = self.Platform.GetFileSystemEntries(strName, "*"); } catch (Exception ex) { throw ToRubyException(ex, strName, DirectoryOperation.Open); } self._dirName = dirname.Clone(); self._pos = -2; return(self); }
/// <summary> /// Returns a collection of fresh MatchData objects. /// </summary> public IList <MatchData> /*!*/ Matches(RubyEncoding kcode, MutableString /*!*/ input, bool inputMayMutate) { string str; MatchCollection matches = Transform(ref kcode, input, 0, out str).Matches(str); var result = new MatchData[matches.Count]; if (result.Length > 0 && inputMayMutate) { // clone and freeze the string once so that it can be shared by all the MatchData objects input = input.Clone().Freeze(); } for (int i = 0; i < result.Length; i++) { result[i] = MatchData.Create(matches[i], input, false, str, kcode, 0); } return(result); }
internal static MatchData Create(Match /*!*/ match, MutableString /*!*/ input, bool freezeInput, string /*!*/ encodedInput, RubyEncoding kcoding, int kStart) { if (!match.Success) { return(null); } int[] kIndices; if (kcoding != null) { // TODO (opt): minimize GetByteCount calls, remove ToCharArray: char[] kCodedChars = encodedInput.ToCharArray(); Encoding encoding = kcoding.StrictEncoding; kIndices = new int[match.Groups.Count * 2]; for (int i = 0; i < match.Groups.Count; i++) { var group = match.Groups[i]; if (group.Success) { // group start index: kIndices[i * 2] = kStart + encoding.GetByteCount(kCodedChars, 0, group.Index); // group length: kIndices[i * 2 + 1] = encoding.GetByteCount(kCodedChars, group.Index, group.Length); } else { kIndices[i * 2] = -1; } } } else { kIndices = null; } if (freezeInput) { input = input.Clone().Freeze(); } return(new MatchData(match, input, kIndices)); }
public MutableString /*!*/ ToMutableString() { return(_string.Clone()); }