/Languages/IronPython/IronPython/Runtime/Operations/StringOps.cs
C# | 2751 lines | 2393 code | 267 blank | 91 comment | 426 complexity | 95b482bb7d0d13067ccac8e62c9c8e63 MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
Large files files are truncated, but you can click here to view the full file
- /* ****************************************************************************
- *
- * Copyright (c) Microsoft Corporation.
- *
- * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
- * copy of the license can be found in the License.html file at the root of this distribution. If
- * you cannot locate the Apache License, Version 2.0, please send an email to
- * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
- * by the terms of the Apache License, Version 2.0.
- *
- * You must not remove this notice, or any other, from this software.
- *
- *
- * ***************************************************************************/
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Globalization;
- using System.Reflection;
- using System.Runtime.InteropServices;
- using System.Text;
- using IronPython.Runtime.Exceptions;
- using IronPython.Runtime.Types;
- using Microsoft.Scripting;
- using Microsoft.Scripting.Runtime;
- using Microsoft.Scripting.Utils;
- #if FEATURE_NUMERICS
- using System.Numerics;
- #else
- using Microsoft.Scripting.Math;
- #endif
- using SpecialNameAttribute = System.Runtime.CompilerServices.SpecialNameAttribute;
- namespace IronPython.Runtime.Operations {
- /// <summary>
- /// ExtensibleString is the base class that is used for types the user defines
- /// that derive from string. It carries along with it the string's value and
- /// our converter recognizes it as a string.
- /// </summary>
- public class ExtensibleString : Extensible<string>, ICodeFormattable, IStructuralEquatable
- #if CLR2
- , IValueEquality
- #endif
- {
- public ExtensibleString() : base(String.Empty) { }
- public ExtensibleString(string self) : base(self) { }
- public override string ToString() {
- return Value;
- }
- #region ICodeFormattable Members
- public virtual string/*!*/ __repr__(CodeContext/*!*/ context) {
- return StringOps.Quote(Value);
- }
- #endregion
- [return: MaybeNotImplemented]
- public object __eq__(object other) {
- if (other is string || other is ExtensibleString || other is Bytes) {
- return ScriptingRuntimeHelpers.BooleanToObject(EqualsWorker(other));
- }
- return NotImplementedType.Value;
- }
- [return: MaybeNotImplemented]
- public object __ne__(object other) {
- if (other is string || other is ExtensibleString || other is Bytes) {
- return ScriptingRuntimeHelpers.BooleanToObject(!EqualsWorker(other));
- }
- return NotImplementedType.Value;
- }
- #region IValueEquality members
- #if CLR2
- int IValueEquality.GetValueHashCode() {
- return GetHashCode();
- }
- bool IValueEquality.ValueEquals(object other) {
- return EqualsWorker(other);
- }
- #endif
- #endregion
- #region IStructuralEquatable Members
- int IStructuralEquatable.GetHashCode(IEqualityComparer comparer) {
- if (comparer is PythonContext.PythonEqualityComparer) {
- return GetHashCode();
- }
- return ((IStructuralEquatable)PythonTuple.MakeTuple(Value.ToCharArray())).GetHashCode(comparer);
- }
- bool IStructuralEquatable.Equals(object other, IEqualityComparer comparer) {
- if (comparer is PythonContext.PythonEqualityComparer) {
- return EqualsWorker(other);
- }
- ExtensibleString es = other as ExtensibleString;
- if (es != null) return EqualsWorker(es.Value, comparer);
- string os = other as string;
- if (os != null) return EqualsWorker(os, comparer);
- Bytes tempBytes = other as Bytes;
- if (tempBytes != null) return EqualsWorker(tempBytes.ToString(), comparer);
- return false;
- }
- private bool EqualsWorker(object other) {
- if (other == null) return false;
- ExtensibleString es = other as ExtensibleString;
- if (es != null) return Value == es.Value;
- string os = other as string;
- if (os != null) return Value == os;
- Bytes tempBytes = other as Bytes;
- if (tempBytes != null) return Value == tempBytes.ToString();
- return false;
- }
- private bool EqualsWorker(string/*!*/ other, IEqualityComparer comparer) {
- Debug.Assert(other != null);
- if (Value.Length != other.Length) {
- return false;
- } else if (Value.Length == 0) {
- // 2 empty strings are equal
- return true;
- }
- for (int i = 0; i < Value.Length; i++) {
- if (!comparer.Equals(Value[i], other[i])) {
- return false;
- }
- }
- return true;
- }
- #endregion
- #region ISequence Members
- public virtual object this[int index] {
- get { return ScriptingRuntimeHelpers.CharToString(Value[index]); }
- }
- public object this[Slice slice] {
- get { return StringOps.GetItem(Value, slice); }
- }
- public object __getslice__(int start, int stop) {
- return StringOps.__getslice__(Value, start, stop);
- }
- #endregion
- #region IPythonContainer Members
- public virtual int __len__() {
- return Value.Length;
- }
- public virtual bool __contains__(object value) {
- if (value is string) return Value.Contains((string)value);
- else if (value is ExtensibleString) return Value.Contains(((ExtensibleString)value).Value);
- else if (value is Bytes) return Value.Contains(value.ToString());
- throw PythonOps.TypeErrorForBadInstance("expected string, got {0}", value);
- }
- #endregion
- }
- /// <summary>
- /// StringOps is the static class that contains the methods defined on strings, i.e. 'abc'
- ///
- /// Here we define all of the methods that a Python user would see when doing dir('abc').
- /// If the user is running in a CLS aware context they will also see all of the methods
- /// defined in the CLS System.String type.
- /// </summary>
- public static class StringOps {
- internal const int LowestUnicodeValue = 0x7f;
- internal static object FastNew(CodeContext/*!*/ context, object x) {
- if (x == null) {
- return "None";
- }
- string xstr = (x as string);
- if (xstr != null) {
- return xstr;
- }
- // we don't invoke PythonOps.StringRepr here because we want to return the
- // Extensible<string> directly back if that's what we received from __str__.
- object value = PythonContext.InvokeUnaryOperator(context, UnaryOperators.String, x);
- if (value is string || value is Extensible<string>) {
- return value;
- }
- throw PythonOps.TypeError("expected str, got {0} from __str__", DynamicHelpers.GetPythonType(value).Name);
- }
- internal static string FastNewUnicode(CodeContext context, object value, object encoding, object errors) {
- string strErrors = errors as string;
- if (strErrors == null) {
- throw PythonOps.TypeError("unicode() argument 3 must be string, not {0}", PythonTypeOps.GetName(errors));
- }
- if (value != null) {
- string strValue = value as string;
- if (strValue != null) {
- return StringOps.RawDecode(context, strValue, encoding, strErrors);
- }
- Extensible<string> es = value as Extensible<string>;
- if (es != null) {
- return StringOps.RawDecode(context, es.Value, encoding, strErrors);
- }
- Bytes bytes = value as Bytes;
- if (bytes != null) {
- return StringOps.RawDecode(context, bytes.ToString(), encoding, strErrors);
- }
- PythonBuffer buffer = value as PythonBuffer;
- if (buffer != null) {
- return StringOps.RawDecode(context, buffer.ToString(), encoding, strErrors);
- }
- }
- throw PythonOps.TypeError("coercing to Unicode: need string or buffer, {0} found", PythonTypeOps.GetName(value));
- }
- internal static object FastNewUnicode(CodeContext context, object value, object encoding) {
- return FastNewUnicode(context, value, encoding, "strict");
- }
- internal static object FastNewUnicode(CodeContext context, object value) {
- if (value == null) {
- return "None";
- } else if (value is string) {
- return value;
- }
- object res;
- OldInstance oi = value as OldInstance;
- if (oi != null &&
- (oi.TryGetBoundCustomMember(context, "__unicode__", out res) || oi.TryGetBoundCustomMember(context, "__str__", out res))) {
- res = context.LanguageContext.Call(context, res);
- if (res is string || res is Extensible<string>) {
- return res;
- }
- throw PythonOps.TypeError("coercing to Unicode: expected string, got {0}", PythonTypeOps.GetName(value));
- }
- if (PythonTypeOps.TryInvokeUnaryOperator(context, value, "__unicode__", out res) ||
- PythonTypeOps.TryInvokeUnaryOperator(context, value, "__str__", out res)) {
- if (res is string || res is Extensible<string>) {
- return res;
- }
- throw PythonOps.TypeError("coercing to Unicode: expected string, got {0}", PythonTypeOps.GetName(value));
- }
- return FastNewUnicode(context, value, context.LanguageContext.DefaultEncoding.WebName, "strict");
- }
- #region Python Constructors
- [StaticExtensionMethod]
- public static object __new__(CodeContext/*!*/ context, PythonType cls) {
- if (cls == TypeCache.String) {
- return "";
- } else {
- return cls.CreateInstance(context);
- }
- }
- [StaticExtensionMethod]
- public static object __new__(CodeContext/*!*/ context, PythonType cls, object @object) {
- if (cls == TypeCache.String) {
- return FastNew(context, @object);
- } else {
- return cls.CreateInstance(context, __new__(context, TypeCache.String, @object));
- }
- }
- [StaticExtensionMethod]
- public static object __new__(CodeContext/*!*/ context, PythonType cls, [NotNull]string @object) {
- if (cls == TypeCache.String) {
- return @object;
- } else {
- return cls.CreateInstance(context, @object);
- }
- }
- [StaticExtensionMethod]
- public static object __new__(CodeContext/*!*/ context, PythonType cls, [NotNull]ExtensibleString @object) {
- if (cls == TypeCache.String) {
- return FastNew(context, @object);
- } else {
- return cls.CreateInstance(context, __new__(context, TypeCache.String, @object));
- }
- }
- [StaticExtensionMethod]
- public static object __new__(CodeContext/*!*/ context, PythonType cls, char @object) {
- if (cls == TypeCache.String) {
- return ScriptingRuntimeHelpers.CharToString(@object);
- } else {
- return cls.CreateInstance(context, __new__(context, TypeCache.String, @object));
- }
- }
- [StaticExtensionMethod]
- public static object __new__(CodeContext/*!*/ context, PythonType cls, [NotNull]BigInteger @object) {
- if (cls == TypeCache.String) {
- return @object.ToString();
- } else {
- return cls.CreateInstance(context, __new__(context, TypeCache.String, @object));
- }
- }
- [StaticExtensionMethod]
- public static object __new__(CodeContext/*!*/ context, PythonType cls, [NotNull]Extensible<BigInteger> @object) {
- if (cls == TypeCache.String) {
- return FastNew(context, @object);
- } else {
- return cls.CreateInstance(context, __new__(context, TypeCache.String, @object));
- }
- }
- [StaticExtensionMethod]
- public static object __new__(CodeContext/*!*/ context, PythonType cls, int @object) {
- if (cls == TypeCache.String) {
- return @object.ToString();
- } else {
- return cls.CreateInstance(context, __new__(context, TypeCache.String, @object));
- }
- }
- [StaticExtensionMethod]
- public static object __new__(CodeContext/*!*/ context, PythonType cls, bool @object) {
- if (cls == TypeCache.String) {
- return @object.ToString();
- } else {
- return cls.CreateInstance(context, __new__(context, TypeCache.String, @object));
- }
- }
- [StaticExtensionMethod]
- public static object __new__(CodeContext/*!*/ context, PythonType cls, double @object) {
- if (cls == TypeCache.String) {
- return DoubleOps.__str__(context, @object);
- } else {
- return cls.CreateInstance(context, __new__(context, TypeCache.String, @object));
- }
- }
- [StaticExtensionMethod]
- public static object __new__(CodeContext/*!*/ context, PythonType cls, Extensible<double> @object) {
- if (cls == TypeCache.String) {
- return FastNew(context, @object);
- } else {
- return cls.CreateInstance(context, __new__(context, TypeCache.String, @object));
- }
- }
- [StaticExtensionMethod]
- public static object __new__(CodeContext/*!*/ context, PythonType cls, float @object) {
- if (cls == TypeCache.String) {
- return SingleOps.__str__(context, @object);
- } else {
- return cls.CreateInstance(context, __new__(context, TypeCache.String, @object));
- }
- }
- [StaticExtensionMethod]
- public static object __new__(CodeContext/*!*/ context, PythonType cls,
- object @string,
- [DefaultParameterValue(null)] string encoding,
- [DefaultParameterValue("strict")] string errors) {
- string str = @string as string;
- if (str == null) throw PythonOps.TypeError("converting to unicode: need string, got {0}", DynamicHelpers.GetPythonType(@string).Name);
- if (cls == TypeCache.String) {
- return decode(context, str, encoding ?? PythonContext.GetContext(context).GetDefaultEncodingName(), errors);
- } else {
- return cls.CreateInstance(context, __new__(context, TypeCache.String, str, encoding, errors));
- }
- }
-
- #endregion
- #region Python __ methods
- public static bool __contains__(string s, [BytesConversion]string item) {
- return s.Contains(item);
- }
- public static bool __contains__(string s, char item) {
- return s.IndexOf(item) != -1;
- }
- public static string __format__(CodeContext/*!*/ context, string self, [BytesConversion]string formatSpec) {
- return ObjectOps.__format__(context, self, formatSpec);
- }
- public static int __len__(string s) {
- return s.Length;
- }
- [SpecialName]
- public static string GetItem(string s, int index) {
- return ScriptingRuntimeHelpers.CharToString(s[PythonOps.FixIndex(index, s.Length)]);
- }
- [SpecialName]
- public static string GetItem(string s, object index) {
- return GetItem(s, Converter.ConvertToIndex(index));
- }
- [SpecialName]
- public static string GetItem(string s, Slice slice) {
- if (slice == null) throw PythonOps.TypeError("string indices must be slices or integers");
- int start, stop, step;
- slice.indices(s.Length, out start, out stop, out step);
- if (step == 1) {
- return stop > start ? s.Substring(start, stop - start) : String.Empty;
- } else {
- int index = 0;
- char[] newData;
- if (step > 0) {
- if (start > stop) return String.Empty;
- int icnt = (stop - start + step - 1) / step;
- newData = new char[icnt];
- for (int i = start; i < stop; i += step) {
- newData[index++] = s[i];
- }
- } else {
- if (start < stop) return String.Empty;
- int icnt = (stop - start + step + 1) / step;
- newData = new char[icnt];
- for (int i = start; i > stop; i += step) {
- newData[index++] = s[i];
- }
- }
- return new string(newData);
- }
- }
- public static string __getslice__(string self, int x, int y) {
- Slice.FixSliceArguments(self.Length, ref x, ref y);
- if (x >= y) return String.Empty;
- return self.Substring(x, y - x);
- }
-
- #endregion
- #region Public Python methods
- /// <summary>
- /// Returns a copy of this string converted to uppercase
- /// </summary>
- public static string capitalize(this string self) {
- if (self.Length == 0) return self;
- return Char.ToUpperInvariant(self[0]) + self.Substring(1).ToLowerInvariant();
- }
- // default fillchar (padding char) is a space
- public static string center(this string self, int width) {
- return center(self, width, ' ');
- }
- public static string center(this string self, int width, char fillchar) {
- int spaces = width - self.Length;
- if (spaces <= 0) return self;
- StringBuilder ret = new StringBuilder(width);
- ret.Append(fillchar, spaces / 2);
- ret.Append(self);
- ret.Append(fillchar, (spaces + 1) / 2);
- return ret.ToString();
- }
- public static int count(this string self, [BytesConversion]string sub) {
- return count(self, sub, 0, self.Length);
- }
- public static int count(this string self, [BytesConversion]string sub, int start) {
- return count(self, sub, start, self.Length);
- }
- public static int count(this string self, [BytesConversion]string ssub, int start, int end) {
- if (ssub == null) throw PythonOps.TypeError("expected string for 'sub' argument, got NoneType");
- if (start > self.Length) {
- return 0;
- }
- start = PythonOps.FixSliceIndex(start, self.Length);
- end = PythonOps.FixSliceIndex(end, self.Length);
- if (ssub.Length == 0) {
- return Math.Max((end - start) + 1, 0);
- }
- int count = 0;
- CompareInfo c = CultureInfo.InvariantCulture.CompareInfo;
- while (true) {
- if (end <= start) break;
- int index = c.IndexOf(self, ssub, start, end - start, CompareOptions.Ordinal);
- if (index == -1) break;
- count++;
- start = index + ssub.Length;
- }
- return count;
- }
- public static string decode(CodeContext/*!*/ context, string s) {
- return decode(context, s, Missing.Value, "strict");
- }
- public static string decode(CodeContext/*!*/ context, string s, [Optional]object encoding, [DefaultParameterValue("strict")]string errors) {
- return RawDecode(context, s, encoding, errors);
- }
- public static string encode(CodeContext/*!*/ context, string s, [Optional]object encoding, [DefaultParameterValue("strict")]string errors) {
- return RawEncode(context, s, encoding, errors);
- }
- private static string CastString(object o) {
- string res = o as string;
- if (res != null) {
- return res;
- }
- return ((Extensible<string>)o).Value;
- }
- internal static string AsString(object o) {
- string res = o as string;
- if (res != null) {
- return res;
- }
- Extensible<string> es = o as Extensible<string>;
- if (es != null) {
- return es.Value;
- }
- return null;
- }
- public static bool endswith(this string self, object suffix) {
- TryStringOrTuple(suffix);
- if (suffix is PythonTuple)
- return endswith(self, (PythonTuple)suffix);
- else
- return endswith(self, CastString(suffix));
- }
- public static bool endswith(this string self, object suffix, int start) {
- TryStringOrTuple(suffix);
- if (suffix is PythonTuple)
- return endswith(self, (PythonTuple)suffix, start);
- else
- return endswith(self, CastString(suffix), start);
- }
- public static bool endswith(this string self, object suffix, int start, int end) {
- TryStringOrTuple(suffix);
- if (suffix is PythonTuple)
- return endswith(self, (PythonTuple)suffix, start, end);
- else
- return endswith(self, CastString(suffix), start, end);
- }
- public static string expandtabs(string self) {
- return expandtabs(self, 8);
- }
- public static string expandtabs(this string self, int tabsize) {
- StringBuilder ret = new StringBuilder(self.Length * 2);
- string v = self;
- int col = 0;
- for (int i = 0; i < v.Length; i++) {
- char ch = v[i];
- switch (ch) {
- case '\n':
- case '\r': col = 0; ret.Append(ch); break;
- case '\t':
- if (tabsize > 0) {
- int tabs = tabsize - (col % tabsize);
- int existingSize = ret.Capacity;
- ret.Capacity = checked(existingSize + tabs);
- ret.Append(' ', tabs);
- col = 0;
- }
- break;
- default:
- col++;
- ret.Append(ch);
- break;
- }
- }
- return ret.ToString();
- }
- public static int find(this string self, [BytesConversion]string sub) {
- if (sub == null) throw PythonOps.TypeError("expected string, got NoneType");
- if (sub.Length == 1) return self.IndexOf(sub[0]);
-
- CompareInfo c = CultureInfo.InvariantCulture.CompareInfo;
- return c.IndexOf(self, sub, CompareOptions.Ordinal);
- }
- public static int find(this string self, [BytesConversion]string sub, int start) {
- if (sub == null) throw PythonOps.TypeError("expected string, got NoneType");
- if (start > self.Length) return -1;
- start = PythonOps.FixSliceIndex(start, self.Length);
- CompareInfo c = CultureInfo.InvariantCulture.CompareInfo;
- return c.IndexOf(self, sub, start, CompareOptions.Ordinal);
- }
- public static int find(this string self, [BytesConversion]string sub, BigInteger start) {
- if (sub == null) throw PythonOps.TypeError("expected string, got NoneType");
- if (start > self.Length) return -1;
- return find(self, sub, (int)start);
- }
- public static int find(this string self, [BytesConversion]string sub, int start, int end) {
- if (sub == null) throw PythonOps.TypeError("expected string, got NoneType");
- if (start > self.Length) return -1;
- start = PythonOps.FixSliceIndex(start, self.Length);
- end = PythonOps.FixSliceIndex(end, self.Length);
- if (end < start) return -1;
- CompareInfo c = CultureInfo.InvariantCulture.CompareInfo;
- return c.IndexOf(self, sub, start, end - start, CompareOptions.Ordinal);
- }
- public static int find(this string self, [BytesConversion]string sub, BigInteger start, BigInteger end) {
- if (sub == null) throw PythonOps.TypeError("expected string, got NoneType");
- if (start > self.Length) return -1;
- return find(self, sub, (int)start, (int)end);
- }
- public static int find(this string self, [BytesConversion]string sub, object start, [DefaultParameterValue(null)]object end) {
- return find(self, sub, CheckIndex(start, 0), CheckIndex(end, self.Length));
- }
- public static int index(this string self, [BytesConversion]string sub) {
- if (sub == null) throw PythonOps.TypeError("expected string, got NoneType");
- return index(self, sub, 0, self.Length);
- }
- public static int index(this string self, [BytesConversion]string sub, int start) {
- if (sub == null) throw PythonOps.TypeError("expected string, got NoneType");
- return index(self, sub, start, self.Length);
- }
- public static int index(this string self, [BytesConversion]string sub, int start, int end) {
- if (sub == null) throw PythonOps.TypeError("expected string, got NoneType");
- int ret = find(self, sub, start, end);
- if (ret == -1) throw PythonOps.ValueError("substring {0} not found in {1}", sub, self);
- return ret;
- }
- public static int index(this string self, [BytesConversion]string sub, object start, [DefaultParameterValue(null)]object end) {
- return index(self, sub, CheckIndex(start, 0), CheckIndex(end, self.Length));
- }
- public static bool isalnum(this string self) {
- if (self.Length == 0) return false;
- string v = self;
- for (int i = v.Length - 1; i >= 0; i--) {
- if (!Char.IsLetterOrDigit(v, i)) return false;
- }
- return true;
- }
- public static bool isalpha(this string self) {
- if (self.Length == 0) return false;
- string v = self;
- for (int i = v.Length - 1; i >= 0; i--) {
- if (!Char.IsLetter(v, i)) return false;
- }
- return true;
- }
- public static bool isdigit(this string self) {
- if (self.Length == 0) return false;
- string v = self;
- for (int i = v.Length - 1; i >= 0; i--) {
- // CPython considers the circled digits to be digits
- if (!Char.IsDigit(v, i) && (v[i] < '\u2460' || v[i] > '\u2468')) return false;
- }
- return true;
- }
- public static bool isspace(this string self) {
- if (self.Length == 0) return false;
- string v = self;
- for (int i = v.Length - 1; i >= 0; i--) {
- if (!Char.IsWhiteSpace(v, i)) return false;
- }
- return true;
- }
- public static bool isdecimal(this string self) {
- return isnumeric(self);
- }
- public static bool isnumeric(this string self) {
- if (String.IsNullOrEmpty(self)) return false;
- foreach (char c in self) {
- if (!Char.IsDigit(c)) return false;
- }
- return true;
- }
- public static bool islower(this string self) {
- if (self.Length == 0) return false;
- string v = self;
- bool hasLower = false;
- for (int i = v.Length - 1; i >= 0; i--) {
- if (!hasLower && Char.IsLower(v, i)) hasLower = true;
- if (Char.IsUpper(v, i)) return false;
- }
- return hasLower;
- }
- public static bool isupper(this string self) {
- if (self.Length == 0) return false;
- string v = self;
- bool hasUpper = false;
- for (int i = v.Length - 1; i >= 0; i--) {
- if (!hasUpper && Char.IsUpper(v, i)) hasUpper = true;
- if (Char.IsLower(v, i)) return false;
- }
- return hasUpper;
- }
- /// <summary>
- /// return true if self is a titlecased string and there is at least one
- /// character in self; also, uppercase characters may only follow uncased
- /// characters (e.g. whitespace) and lowercase characters only cased ones.
- /// return false otherwise.
- /// </summary>
- public static bool istitle(this string self) {
- if (self == null || self.Length == 0) return false;
- string v = self;
- bool prevCharCased = false, currCharCased = false, containsUpper = false;
- for (int i = 0; i < v.Length; i++) {
- if (Char.IsUpper(v, i) || CharUnicodeInfo.GetUnicodeCategory(v, i) == UnicodeCategory.TitlecaseLetter) {
- containsUpper = true;
- if (prevCharCased)
- return false;
- else
- currCharCased = true;
- } else if (Char.IsLower(v, i))
- if (!prevCharCased)
- return false;
- else
- currCharCased = true;
- else
- currCharCased = false;
- prevCharCased = currCharCased;
- }
- // if we've gone through the whole string and haven't encountered any rule
- // violations but also haven't seen an Uppercased char, then this is not a
- // title e.g. '\n', all whitespace etc.
- return containsUpper;
- }
- public static bool isunicode(this string self) {
- foreach (char c in self) {
- if (c >= LowestUnicodeValue) return true;
- }
- return false;
- }
- /// <summary>
- /// Return a string which is the concatenation of the strings
- /// in the sequence seq. The separator between elements is the
- /// string providing this method
- /// </summary>
- public static string join(this string self, object sequence) {
- IEnumerator seq = PythonOps.GetEnumerator(sequence);
- if (!seq.MoveNext()) return "";
- // check if we have just a sequence of just one value - if so just
- // return that value.
- object curVal = seq.Current;
- if (!seq.MoveNext()) return Converter.ConvertToString(curVal);
- StringBuilder ret = new StringBuilder();
- AppendJoin(curVal, 0, ret);
- int index = 1;
- do {
- ret.Append(self);
- AppendJoin(seq.Current, index, ret);
- index++;
- } while (seq.MoveNext());
- return ret.ToString();
- }
- public static string join(this string/*!*/ self, [NotNull]List/*!*/ sequence) {
- if (sequence.__len__() == 0) return String.Empty;
- lock (sequence) {
- if (sequence.__len__() == 1) {
- return Converter.ConvertToString(sequence[0]);
- }
- StringBuilder ret = new StringBuilder();
- AppendJoin(sequence._data[0], 0, ret);
- for (int i = 1; i < sequence._size; i++) {
- if (!String.IsNullOrEmpty(self)) {
- ret.Append(self);
- }
- AppendJoin(sequence._data[i], i, ret);
- }
- return ret.ToString();
- }
- }
- public static string ljust(this string self, int width) {
- return ljust(self, width, ' ');
- }
- public static string ljust(this string self, int width, char fillchar) {
- if (width < 0) return self;
- int spaces = width - self.Length;
- if (spaces <= 0) return self;
- StringBuilder ret = new StringBuilder(width);
- ret.Append(self);
- ret.Append(fillchar, spaces);
- return ret.ToString();
- }
- // required for better match with cpython upper/lower
- private static CultureInfo CasingCultureInfo = new CultureInfo("en");
- public static string lower(this string self) {
- return CasingCultureInfo.TextInfo.ToLower(self);
- }
- internal static string ToLowerAsciiTriggered(this string self) {
- for (int i = 0; i < self.Length; i++) {
- if (self[i] >= 'A' && self[i] <= 'Z') {
- return self.ToLowerInvariant();
- }
- }
- return self;
- }
- public static string lstrip(this string self) {
- return self.TrimStart();
- }
- public static string lstrip(this string self, [BytesConversion]string chars) {
- if (chars == null) return lstrip(self);
- return self.TrimStart(chars.ToCharArray());
- }
- [return: SequenceTypeInfo(typeof(string))]
- public static PythonTuple partition(this string self, [BytesConversion]string sep) {
- if (sep == null)
- throw PythonOps.TypeError("expected string, got NoneType");
- if (sep.Length == 0)
- throw PythonOps.ValueError("empty separator");
- object[] obj = new object[3] { "", "", "" };
- if (self.Length != 0) {
- int index = find(self, sep);
- if (index == -1) {
- obj[0] = self;
- } else {
- obj[0] = self.Substring(0, index);
- obj[1] = sep;
- obj[2] = self.Substring(index + sep.Length, self.Length - index - sep.Length);
- }
- }
- return new PythonTuple(obj);
- }
- public static string replace(this string self, [BytesConversion]string old, [BytesConversion]string @new,
- [DefaultParameterValue(-1)]int count) {
- if (old == null) {
- throw PythonOps.TypeError("expected a character buffer object"); // cpython message
- }
- if (old.Length == 0) return ReplaceEmpty(self, @new, count);
- string v = self;
- int replacements = StringOps.count(v, old);
- replacements = (count < 0 || count > replacements) ? replacements : count;
- int newLength = v.Length;
- newLength -= replacements * old.Length;
- newLength = checked(newLength + replacements * @new.Length);
- StringBuilder ret = new StringBuilder(newLength);
- int index;
- int start = 0;
- while (count != 0 && (index = v.IndexOf(old, start, StringComparison.Ordinal)) != -1) {
- ret.Append(v, start, index - start);
- ret.Append(@new);
- start = index + old.Length;
- count--;
- }
- ret.Append(v.Substring(start));
- return ret.ToString();
- }
- public static int rfind(this string self, [BytesConversion]string sub) {
- if (sub == null) throw PythonOps.TypeError("expected string, got NoneType");
- return rfind(self, sub, 0, self.Length);
- }
- public static int rfind(this string self, [BytesConversion]string sub, int start) {
- if (sub == null) throw PythonOps.TypeError("expected string, got NoneType");
- if (start > self.Length) return -1;
- return rfind(self, sub, start, self.Length);
- }
- public static int rfind(this string self, [BytesConversion]string sub, BigInteger start) {
- if (sub == null) throw PythonOps.TypeError("expected string, got NoneType");
- if (start > self.Length) return -1;
- return rfind(self, sub, (int)start, self.Length);
- }
- public static int rfind(this string self, [BytesConversion]string sub, int start, int end) {
- if (sub == null) throw PythonOps.TypeError("expected string, got NoneType");
- if (start > self.Length) return -1;
- start = PythonOps.FixSliceIndex(start, self.Length);
- end = PythonOps.FixSliceIndex(end, self.Length);
- if (start > end) return -1; // can't possibly match anything, not even an empty string
- if (sub.Length == 0) return end; // match at the end
- if (end == 0) return -1; // can't possibly find anything
- CompareInfo c = CultureInfo.InvariantCulture.CompareInfo;
- return c.LastIndexOf(self, sub, end - 1, end - start, CompareOptions.Ordinal);
- }
- public static int rfind(this string self, [BytesConversion]string sub, BigInteger start, BigInteger end) {
- if (sub == null) throw PythonOps.TypeError("expected string, got NoneType");
- if (start > self.Length) return -1;
- return rfind(self, sub, (int)start, (int)end);
- }
- public static int rfind(this string self, [BytesConversion]string sub, object start, [DefaultParameterValue(null)]object end) {
- return rfind(self, sub, CheckIndex(start, 0), CheckIndex(end, self.Length));
- }
- public static int rindex(this string self, [BytesConversion]string sub) {
- return rindex(self, sub, 0, self.Length);
- }
- public static int rindex(this string self, [BytesConversion]string sub, int start) {
- return rindex(self, sub, start, self.Length);
- }
- public static int rindex(this string self, [BytesConversion]string sub, int start, int end) {
- int ret = rfind(self, sub, start, end);
- if (ret == -1) throw PythonOps.ValueError("substring {0} not found in {1}", sub, self);
- return ret;
- }
- public static int rindex(this string self, [BytesConversion]string sub, object start, [DefaultParameterValue(null)]object end) {
- return rindex(self, sub, CheckIndex(start, 0), CheckIndex(end, self.Length));
- }
- public static string rjust(this string self, int width) {
- return rjust(self, width, ' ');
- }
- public static string rjust(this string self, int width, char fillchar) {
- int spaces = width - self.Length;
- if (spaces <= 0) return self;
- StringBuilder ret = new StringBuilder(width);
- ret.Append(fillchar, spaces);
- ret.Append(self);
- return ret.ToString();
- }
- [return: SequenceTypeInfo(typeof(string))]
- public static PythonTuple rpartition(this string self, [BytesConversion]string sep) {
- if (sep == null)
- throw PythonOps.TypeError("expected string, got NoneType");
- if (sep.Length == 0)
- throw PythonOps.ValueError("empty separator");
- object[] obj = new object[3] { "", "", "" };
- if (self.Length != 0) {
- int index = rfind(self, sep);
- if (index == -1) {
- obj[2] = self;
- } else {
- obj[0] = self.Substring(0, index);
- obj[1] = sep;
- obj[2] = self.Substring(index + sep.Length, self.Length - index - sep.Length);
- }
- }
- return new PythonTuple(obj);
- }
- // when no maxsplit arg is given then just use split
- public static List rsplit(this string self) {
- return SplitInternal(self, (char[])null, -1);
- }
- public static List rsplit(this string self, [BytesConversion]string sep) {
- return rsplit(self, sep, -1);
- }
- public static List rsplit(this string self, [BytesConversion]string sep, int maxsplit) {
- // rsplit works like split but needs to split from the right;
- // reverse the original string (and the sep), split, reverse
- // the split list and finally reverse each element of the list
- string reversed = Reverse(self);
- if (sep != null) sep = Reverse(sep);
- List temp = null, ret = null;
- temp = split(reversed, sep, maxsplit);
- temp.reverse();
- int resultlen = temp.__len__();
- if (resultlen != 0) {
- ret = new List(resultlen);
- foreach (string s in temp)
- ret.AddNoLock(Reverse(s));
- } else {
- ret = temp;
- }
- return ret;
- }
- public static string rstrip(this string self) {
- return self.TrimEnd();
- }
- public static string rstrip(this string self, [BytesConversion]string chars) {
- if (chars == null) return rstrip(self);
- return self.TrimEnd(chars.ToCharArray());
- }
- public static List split(this string self) {
- return SplitInternal(self, (char[])null, -1);
- }
- public static List split(this string self, [BytesConversion]string sep) {
- return split(self, sep, -1);
- }
- public static List split(this string self, [BytesConversion]string sep, int maxsplit) {
- if (sep == null) {
- if (maxsplit == 0) {
- // Corner case for CPython compatibility
- List result = PythonOps.MakeEmptyList(1);
- result.AddNoLock(self.TrimStart());
- return result;
-
- } else {
- return SplitInternal(self, (char[])null, maxsplit);
- }
- }
- if (sep.Length == 0) {
- throw PythonOps.ValueError("empty separator");
- } else if (sep.Length == 1) {
- return SplitInternal(self, new char[] { sep[0] }, maxsplit);
- } else {
- return SplitInternal(self, sep, maxsplit);
- }
- }
- public static List splitlines(this string self) {
- return splitlines(self, false);
- }
- public static List splitlines(this string self, bool keepends) {
- List ret = new List();
- int i, linestart;
- for (i = 0, linestart = 0; i < self.Length; i++) {
- if (self[i] == '\n' || self[i] == '\r' || self[i] == '\x2028') {
- // special case of "\r\n" as end of line marker
- if (i < self.Length - 1 && self[i] == '\r' && self[i + 1] == '\n') {
- if (keepends)
- ret.AddNoLock(self.Substring(linestart, i - linestart + 2));
- else
- ret.AddNoLock(self.Substring(linestart, i - linestart));
- linestart = i + 2;
- i++;
- } else { //'\r', '\n', or unicode new line as end of line marker
- if (keepends)
- ret.AddNoLock(self.Substring(linestart, i - linestart + 1));
- else
- ret.AddNoLock(self.Substring(linestart, i - linestart));
- linestart = i + 1;
- }
- }
- }
- // the last line needs to be accounted for if it is not empty
- if (i - linestart != 0)
- ret.AddNoLock(self.Substring(linestart, i - linestart));
- return ret;
- }
- public static bool startswith(this string self, object prefix) {
- TryStringOrTuple(prefix);
- if (prefix is PythonTuple)
- return startswith(self, (PythonTuple)prefix);
- else
- return startswith(self, CastString(prefix));
- }
- public static bool startswith(this string self, object prefix, int start) {
- TryStringOrTuple(prefix);
- if (prefix is PythonTuple)
- return startswith(self, (PythonTuple)prefix, start);
- else
- return startswith(self, CastString(prefix), start);
- }
- public static bool startswith(this string self, object prefix, int start, int end) {
- TryStringOrTuple(prefix);
- if (prefix is PythonTuple)
- return startswith(self, (PythonTuple)prefix, start, end);
- else
- return startswith(self, CastString(prefix), start, end);
- }
- public static string strip(this string self) {
- return self.Trim();
- }
- public static string strip(this string self, [BytesConversion]string chars) {
- if (chars == null) return strip(self);
- return self.Trim(chars.ToCharArray());
- }
- public static string swapcase(this string self) {
- StringBuilder ret = new StringBuilder(self);
- for (int i = 0; i < ret.Length; i++) {
- char ch = ret[i];
- if (Char.IsUpper(ch)) ret[i] = Char.ToLowerInvariant(ch);
- else if (Char.IsLower(ch)) ret[i] = Char.ToUpperInvariant(ch);
- }
- return ret.ToString();
- }
- public static string title(this string self) {
- if (self == null || self.Length == 0) return self;
- char[] retchars = self.ToCharArray();
- bool prevCharCased = false;
- bool currCharCased = false;
- int i = 0;
- do {
- if (Char.IsUpper(retchars[i]) || Char.IsLower(retchars[i])) {
- if (!prevCharCased)
- retchars[i] = Char.ToUpperInvariant(retchars[i]);
- else
- retchars[i] = Char.ToLowerInvariant(retchars[i]);
- currCharCased = true;
- } else {
- currCharCased = false;
- }
- i++;
- prevCharCased = currCharCased;
- }
- while (i < retchars.Length);
- return new string(retchars);
- }
- //translate on a unicode string differs from that on an ascii
- //for unicode, the table argument is actually a dictionary with
- //character ordinals as keys and the replacement strings as values
- public static string translate(this string self, [NotNull]PythonDictionary table) {
- if (table == null || self.Length == 0) {
- return self;
- }
- StringBuilder ret = new StringBuilder();
- for (int i = 0, idx = 0; i < self.Length; i++) {
- idx = (int)self[i];
- if (table.__contains__(idx)) {
- var mapped = table[idx];
- if (mapped == null) {
- continue;
- }
- if (mapped is int) {
- var mappedInt = (int) mapped;
- if (mappedInt > 0xFFFF) {
- throw PythonOps.TypeError("character mapping must be in range(0x%lx)");
- }
- ret.Append((char)(int)mapped);
- } else if (mapped is String) {
- ret.Append(mapped);
- } else {
- throw PythonOps.TypeError("character mapping must return integer, None or unicode");
- }
- } else {
- ret.Append(self[i]);
- }
- }
- return ret.ToString();
- }
- public static string translate(this string self, [BytesConversion]string table) {
- return translate(self, table, (string)null);
- }
- public static string translate(this string self, [BytesConversion]string table, [BytesConversion]string deletechars) {
- if (table != null && table.Length != 256) {
- throw PythonOps.ValueError("translation table must be 256 characters long");
- } else if (self.Length == 0) {
- return self;
- }
- // List<char> is about 2/3rds as expensive as StringBuilder appending individual
- // char's so we use that instead of a StringBuilder
- List<char> res = new List<char>();
- for (int i = 0; i < self.Length; i++) {
- if (deletechars == null || !deletechars.Contains(Char.ToString(self[i]))) {
- if (table != null) {
- int idx = (int)self[i];
- if (idx >= 0 && idx < 256) {
- res.Add(table[idx]);
- }
- } else {
- res.Add(self[i]);
- }
- }
- }
- return new String(res.ToArray());
- }
- public static string upper(this string self) {
- return CasingCultureInfo.TextInfo.ToUpper(self);
- }
- public static string zfill(this string self, int width) {
- int spaces = width - self.Length;
- if (spaces <= 0) return self;
- StringBuilder ret = new StringBuilder(width);
- if (self.Length > 0 &&…
Large files files are truncated, but you can click here to view the full file