public SqlString Concat(ISqlString other) { if (other == null || other.IsNull) { return(this); } if (other is SqlString) { var otheString = (SqlString)other; var length = (int)(Length + otheString.Length); if (length >= MaxLength) { throw new ArgumentException("The final string will be over the maximum length"); } var sourceChars = ToCharArray(); var otherChars = otheString.ToCharArray(); var destChars = new char[length]; Array.Copy(sourceChars, 0, destChars, 0, (int)Length); Array.Copy(otherChars, 0, destChars, (int)Length, (int)otheString.Length); return(new SqlString(destChars, length)); } var sb = new StringBuilder(Int16.MaxValue); using (var output = new StringWriter(sb)) { // First read the current stream using (var reader = GetInput(Encoding.Unicode)) { var buffer = new char[2048]; int count; while ((count = reader.Read(buffer, 0, buffer.Length)) != 0) { output.Write(buffer, 0, count); } } // Then read the second stream using (var reader = other.GetInput(Encoding.Unicode)) { var buffer = new char[2048]; int count; while ((count = reader.Read(buffer, 0, buffer.Length)) != 0) { output.Write(buffer, 0, count); } } output.Flush(); } #if PCL var s = sb.ToString(); return(new SqlString(s)); #else var outChars = new char[sb.Length]; sb.CopyTo(0, outChars, 0, sb.Length); return(new SqlString(outChars, outChars.Length)); #endif }
public static SqlXmlNode XmlType(ISqlString s) { var len = s.Length; var content = new char[len]; var offset = 0; const int bufferSize = 1024 * 10; using (var reader = s.GetInput(s.Encoding)) { while (true) { var buffer = new char[bufferSize]; var readCount = reader.Read(buffer, 0, bufferSize); if (readCount == 0) { break; } Array.Copy(buffer, 0, content, offset, readCount); offset += readCount; } } var bytes = s.Encoding.GetBytes(content); if (!s.Encoding.Equals(Encoding.UTF8)) { bytes = Encoding.Convert(s.Encoding, Encoding.UTF8, bytes); } return(new SqlXmlNode(bytes)); }
private static int LexicographicalOrder(ISqlString str1, ISqlString str2) { // If both strings are small use the 'toString' method to compare the // strings. This saves the overhead of having to store very large string // objects in memory for all comparisons. long str1Size = str1.Length; long str2Size = str2.Length; if (str1Size < 32 * 1024 && str2Size < 32 * 1024) { return(String.Compare(str1.ToString(), str2.ToString(), StringComparison.Ordinal)); } // TODO: pick one of the two encodings? // The minimum size long size = System.Math.Min(str1Size, str2Size); TextReader r1 = str1.GetInput(); TextReader r2 = str2.GetInput(); try { try { while (size > 0) { int c1 = r1.Read(); int c2 = r2.Read(); if (c1 != c2) { return(c1 - c2); } --size; } // They compare equally up to the limit, so now compare sizes, if (str1Size > str2Size) { // If str1 is larger return(1); } else if (str1Size < str2Size) { // If str1 is smaller return(-1); } // Must be equal return(0); } finally { r1.Dispose(); r2.Dispose(); } } catch (IOException e) { throw new Exception("IO Error: " + e.Message); } }
private static int LexicographicalOrder(ISqlString str1, ISqlString str2) { // If both strings are small use the 'toString' method to compare the // strings. This saves the overhead of having to store very large string // objects in memory for all comparisons. long str1Size = str1.Length; long str2Size = str2.Length; if (str1Size < 32 * 1024 && str2Size < 32 * 1024) { return String.Compare(str1.ToString(), str2.ToString(), StringComparison.Ordinal); } // TODO: pick one of the two encodings? // The minimum size long size = System.Math.Min(str1Size, str2Size); TextReader r1 = str1.GetInput(str1.Encoding); TextReader r2 = str2.GetInput(str2.Encoding); try { try { while (size > 0) { int c1 = r1.Read(); int c2 = r2.Read(); if (c1 != c2) { return c1 - c2; } --size; } // They compare equally up to the limit, so now compare sizes, if (str1Size > str2Size) { // If str1 is larger return 1; } else if (str1Size < str2Size) { // If str1 is smaller return -1; } // Must be equal return 0; } finally { r1.Dispose(); r2.Dispose(); } } catch (IOException e) { throw new Exception("IO Error: " + e.Message); } }
public static SqlXmlNode XmlType(ISqlString s) { var len = s.Length; var content = new char[len]; var offset = 0; const int bufferSize = 1024*10; using (var reader = s.GetInput(s.Encoding)) { while (true) { var buffer = new char[bufferSize]; var readCount = reader.Read(buffer, 0, bufferSize); if (readCount == 0) break; Array.Copy(buffer, 0, content, offset, readCount); offset += readCount; } } var bytes = s.Encoding.GetBytes(content); if (!s.Encoding.Equals(Encoding.UTF8)) bytes = Encoding.Convert(s.Encoding, Encoding.UTF8, bytes); return new SqlXmlNode(bytes); }