public static string GetSubstring(string /*!*/ self, [DefaultProtocol] int start, [DefaultProtocol] int count) { if (!MutableStringOps.NormalizeSubstringRange(self.Length, ref start, ref count)) { return((start == self.Length) ? self : null); } return(self.Substring(start, count)); }
public static string GetSubstring(ConversionStorage <int> /*!*/ fixnumCast, string /*!*/ self, [NotNull] Range /*!*/ range) { int begin, count; if (!MutableStringOps.NormalizeSubstringRange(fixnumCast, range, self.Length, out begin, out count)) { return(null); } return((count < 0) ? self : GetSubstring(self, begin, count)); }
public override string ToString() { // #<StringScanner 0/11 @ "test ..."> byte[] scanstr = ScanString.ToByteArray(); StringBuilder sb = new StringBuilder("#<StringScanner "); if (CurrentPosition >= Length || CurrentPosition < 0) { sb.Append("fin >"); return(sb.ToString()); } sb.AppendFormat("{0}/{1}", CurrentPosition, scanstr.Length); if (CurrentPosition > 0) { sb.Append(" \""); int len = CurrentPosition; if (len > 5) { len = 5; sb.Append("..."); } for (int i = CurrentPosition - len; i < CurrentPosition; i++) { MutableStringOps.AppendStringRepresentationOfChar(sb, scanstr[i], -1, true); } sb.Append('"'); } sb.Append(" @ "); if (CurrentPosition < scanstr.Length) { int len = scanstr.Length - CurrentPosition; bool ellipsis = false; if (len > 5) { len = 5; ellipsis = true; } sb.Append('"'); for (int i = CurrentPosition; i < CurrentPosition + len; i++) { MutableStringOps.AppendStringRepresentationOfChar(sb, scanstr[i], -1, true); } if (ellipsis) { sb.Append("..."); } sb.Append('"'); } sb.Append('>'); return(sb.ToString()); }
public static MutableString /*!*/ Inspect(string /*!*/ self) { StringBuilder result = new StringBuilder(); result.Append('"'); for (int i = 0; i < self.Length; i++) { MutableStringOps.AppendStringRepresentationOfChar(result, self[i], i + 1 < self.Length ? self[i + 1] : -1, false); } result.Append('"'); return(MutableString.Create(result.ToString())); }
private void Inspect1() { const char sq = '\''; var sjisEncoding = RubyEncoding.KCodeSJIS.StrictEncoding; // あ var sjisWide = new byte[] { 0x82, 0xa0 }; // \u{12345} in UTF-8: var utf8 = new byte[] { 0xF0, 0x92, 0x8D, 0x85 }; // surrogates: U+d808 U+df45 var utf16 = Encoding.UTF8.GetString(utf8); string s; s = MutableStringOps.GetQuotedStringRepresentation(MutableString.CreateBinary(utf8, RubyEncoding.Binary), Context, false, sq).ToString(); Assert(s == @"'\360\222\215\205'"); s = MutableStringOps.GetQuotedStringRepresentation(MutableString.CreateBinary(utf8, RubyEncoding.Binary), Context, true, sq).ToString(); Assert(s == @"'\360\222\215\205'"); Context.KCode = RubyEncoding.KCodeUTF8; s = MutableStringOps.GetQuotedStringRepresentation(MutableString.CreateBinary(utf8, RubyEncoding.KCodeUTF8), Context, false, sq).ToString(); Assert(s == "'" + utf16 + "'"); // incomplete character: Context.KCode = RubyEncoding.KCodeUTF8; s = MutableStringOps.GetQuotedStringRepresentation(MutableString.CreateBinary(new byte[] { 0xF0, 0x92 }, RubyEncoding.KCodeUTF8), Context, false, sq).ToString(); Assert(s == @"'\360\222'"); Context.KCode = RubyEncoding.KCodeUTF8; s = MutableStringOps.GetQuotedStringRepresentation(MutableString.CreateBinary(utf8, RubyEncoding.KCodeSJIS), Context, false, sq).ToString(); Assert(s == "'" + utf16 + "'"); Context.KCode = RubyEncoding.KCodeUTF8; s = MutableStringOps.GetQuotedStringRepresentation(MutableString.CreateBinary(utf8, RubyEncoding.KCodeUTF8), Context, true, sq).ToString(); Assert(s == @"'\360\222\215\205'"); Context.KCode = RubyEncoding.KCodeSJIS; s = MutableStringOps.GetQuotedStringRepresentation(MutableString.CreateBinary(sjisWide, RubyEncoding.KCodeSJIS), Context, false, sq).ToString(); Assert(s == @"'あ'"); Context.KCode = RubyEncoding.KCodeSJIS; s = MutableStringOps.GetQuotedStringRepresentation(MutableString.CreateBinary(sjisWide, RubyEncoding.Binary), Context, false, sq).ToString(); Assert(s == @"'あ'"); Context.KCode = RubyEncoding.KCodeSJIS; s = MutableStringOps.GetQuotedStringRepresentation(MutableString.CreateBinary(sjisWide, RubyEncoding.KCodeUTF8), Context, true, sq).ToString(); Assert(s == @"'\202\240'"); }
private void Inspect2_SJIS() { const char sq = '\''; // あ var sjisWide = new byte[] { 0x82, 0xa0 }; var sjisEncoding = RubyEncoding.SJIS; string s; s = MutableStringOps.GetQuotedStringRepresentation(MutableString.CreateBinary(sjisWide, sjisEncoding), false, sq).ToString(); Assert(s == @"'\x82\xA0'"); s = MutableStringOps.GetQuotedStringRepresentation(MutableString.CreateBinary(sjisWide, sjisEncoding), true, sq).ToString(); Assert(s == @"'\x82\xA0'"); }
/// <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); }
/// <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()); } }
private void Inspect2() { const char sq = '\''; var sjisEncoding = RubyEncoding.SJIS; // あ var sjisWide = new byte[] { 0x82, 0xa0 }; // \u{12345} in UTF-8: var utf8 = new byte[] { 0xF0, 0x92, 0x8D, 0x85 }; // \u{12345} in UTF-16: U+d808 U+df45 var utf16 = Encoding.UTF8.GetString(utf8); string s; s = MutableStringOps.GetQuotedStringRepresentation(MutableString.CreateBinary(utf8, RubyEncoding.Binary), false, sq).ToString(); Assert(s == @"'\xF0\x92\x8D\x85'"); s = MutableStringOps.GetQuotedStringRepresentation(MutableString.CreateBinary(utf8, RubyEncoding.Binary), true, sq).ToString(); Assert(s == @"'\xF0\x92\x8D\x85'"); s = MutableStringOps.GetQuotedStringRepresentation(MutableString.CreateBinary(utf8, RubyEncoding.UTF8), false, sq).ToString(); Assert(s == @"'\u{12345}'"); s = MutableStringOps.GetQuotedStringRepresentation(MutableString.CreateBinary(utf8, RubyEncoding.UTF8), true, sq).ToString(); Assert(s == @"'\u{12345}'"); // incomplete character: s = MutableStringOps.GetQuotedStringRepresentation(MutableString.Create("\ud808\udf45\ud808", RubyEncoding.UTF8), false, sq).ToString(); Assert(s == @"'\u{12345}\u{d808}'"); s = MutableStringOps.GetQuotedStringRepresentation(MutableString.CreateBinary(sjisWide, sjisEncoding), false, sq).ToString(); Assert(s == @"'\x82\xA0'"); s = MutableStringOps.GetQuotedStringRepresentation(MutableString.CreateBinary(sjisWide, sjisEncoding), true, sq).ToString(); Assert(s == @"'\x82\xA0'"); }
public static object Match(BinaryOpStorageWithScope /*!*/ storage, RubyScope /*!*/ scope, RubySymbol /*!*/ self, [DefaultProtocol, NotNull] MutableString /*!*/ pattern) { return(MutableStringOps.Match(storage, scope, self.String.Clone(), pattern)); }
public static object Match(BinaryOpStorageWithScope /*!*/ storage, RubyScope /*!*/ scope, RubySymbol /*!*/ self, [NotNull] RubyRegex /*!*/ regex) { return(MutableStringOps.Match(storage, scope, self.String.Clone(), regex)); }
public static object Match(BinaryOpStorageWithScope /*!*/ storage, RubyScope /*!*/ scope, RubySymbol /*!*/ self, object obj) { return(MutableStringOps.Match(storage, scope, self.String.Clone(), obj)); }
public static MutableString GetSubstring(RubySymbol /*!*/ self, [NotNull] MutableString /*!*/ searchStr) { return(MutableStringOps.GetSubstring(self.String, searchStr)); }
public static RubySymbol /*!*/ Succ(RubyContext /*!*/ context, RubySymbol /*!*/ self) { return(context.CreateSymbol(MutableStringOps.Succ(self.String))); }
public static MutableString GetSubstring(ConversionStorage <int> /*!*/ fixnumCast, RubySymbol /*!*/ self, [NotNull] Range /*!*/ range) { return(MutableStringOps.GetSubstring(fixnumCast, self.String, range)); }
public static object Match(BinaryOpStorageWithScope /*!*/ storage, RubyScope /*!*/ scope, ClrName /*!*/ self, [NotNull] RubyRegex /*!*/ regex) { return(MutableStringOps.Match(storage, scope, GetRubyName(scope.RubyContext, self), regex)); }
public static object Compare(BinaryOpStorage /*!*/ comparisonStorage, RespondToStorage /*!*/ respondToStorage, ClrName /*!*/ self, object other) { return(MutableStringOps.Compare(comparisonStorage, respondToStorage, self.MangledName, other)); }
public static string Insert(string /*!*/ self, [DefaultProtocol] int start, [DefaultProtocol, NotNull] string /*!*/ value) { return(self.Insert(MutableStringOps.NormalizeInsertIndex(start, self.Length), value)); }
public static MutableString GetSubstring(RubyScope /*!*/ scope, RubySymbol /*!*/ self, [NotNull] RubyRegex /*!*/ regex, [DefaultProtocol] int occurrance) { return(MutableStringOps.GetSubstring(scope, self.String, regex, occurrance)); }
public static object GetChar(string /*!*/ self, [DefaultProtocol] int index) { return(MutableStringOps.InExclusiveRangeNormalized(self.Length, ref index) ? RubyUtils.CharToObject(self[index]) : null); }
public static bool Equals(RespondToStorage /*!*/ respondToStorage, BinaryOpStorage /*!*/ equalsStorage, string /*!*/ self, object other) { return(MutableStringOps.Equals(respondToStorage, equalsStorage, self, other)); }
public static MutableString GetSubstring(RubyScope /*!*/ scope, RubySymbol /*!*/ self, [NotNull] RubyRegex /*!*/ regex) { return(MutableStringOps.GetSubstring(scope, self.String, regex)); }
public static RubyArray Unpack(MutableString str, string formatString) { var format = MutableString.Create(formatString); return(MutableStringOps.Unpack(str, format)); }
public static object Match(RubyScope /*!*/ scope, RubySymbol /*!*/ self, [NotNull] RubyRegex /*!*/ regex) { return(MutableStringOps.Match(scope, self.ToMutableString(), regex)); }
public static RubyArray /*!*/ Split(ConversionStorage <MutableString> /*!*/ stringCast, string /*!*/ self) { return(MutableStringOps.Split(stringCast, MutableString.Create(self, RubyEncoding.UTF8), (MutableString)null, 0)); }
public static RubySymbol /*!*/ Capitalize(RubyContext /*!*/ context, RubySymbol /*!*/ self) { return(context.CreateSymbol(MutableStringOps.Capitalize(self.String))); }
public static object Match(BinaryOpStorageWithScope /*!*/ storage, RubyScope /*!*/ scope, ClrName /*!*/ self, object obj) { return(MutableStringOps.Match(storage, scope, GetRubyName(scope.RubyContext, self), obj)); }
public static RubyArray /*!*/ Split(ConversionStorage <MutableString> /*!*/ stringCast, string /*!*/ self, [DefaultProtocol] string separator, [DefaultProtocol, Optional] int limit) { return(MutableStringOps.Split(stringCast, MutableString.Create(self, RubyEncoding.UTF8), MutableString.Create(separator, RubyEncoding.UTF8), limit)); }
public static object Match(BinaryOpStorageWithScope /*!*/ storage, RubyScope /*!*/ scope, ClrName /*!*/ self, [DefaultProtocol, NotNull] MutableString /*!*/ pattern) { return(MutableStringOps.Match(storage, scope, GetRubyName(scope.RubyContext, self), pattern)); }
public static RubyArray /*!*/ Split(ConversionStorage <MutableString> /*!*/ stringCast, string /*!*/ self, [NotNull] RubyRegex /*!*/ regexp, [DefaultProtocol, Optional] int limit) { return(MutableStringOps.Split(stringCast, MutableString.Create(self, RubyEncoding.UTF8), regexp, limit)); }