static void Main() { StringOps so = new StringOps(); StrMod strOp = so.ReplaceSpaces; strOp += so.RemoveSpaces; strOp += so.Reverse; string str = strOp("This is a test."); Console.WriteLine(str); Console.WriteLine(); }
// ps1 and ps2 are set by PythonContext and only on the initial load public static void setdefaultencoding(CodeContext context, object name) { if (name == null) { throw PythonOps.TypeError("name cannot be None"); } string strName = name as string; if (strName == null) { throw PythonOps.TypeError("name must be a string"); } PythonContext pc = context.LanguageContext; Encoding enc; if (!StringOps.TryGetEncoding(strName, out enc)) { throw PythonOps.LookupError("'{0}' does not match any available encodings", strName); } pc.DefaultEncoding = enc; }
/////////////////////////////////////////////////////////////////////// public static StringList GetRange( IList list, int firstIndex, int lastIndex, bool nullIfEmpty ) { if (list == null) { return(null); } StringList range = null; if (firstIndex == Index.Invalid) { firstIndex = 0; } if (lastIndex == Index.Invalid) { lastIndex = list.Count - 1; } if ((!nullIfEmpty || ((list.Count > 0) && ((lastIndex - firstIndex) > 0)))) { range = new StringList(); for (int index = firstIndex; index <= lastIndex; index++) { range.Add(StringOps.GetStringFromObject(list[index])); } } return(range); }
private void AppendString() { string s; if (!_isUnicodeString) { s = PythonOps.ToString(_context, _opts.Value); } else { object o = StringOps.FastNewUnicode(_context, _opts.Value); s = o as string; if (s == null) { s = ((Extensible <string>)o).Value; } } if (s == null) { s = "None"; } AppendString(s); }
public static object encode(CodeContext /*!*/ context, object?obj, [NotNull, DisallowNull] string?encoding = null, [NotNull] string errors = "strict") { if (encoding == null) { if (obj is string str) { PythonContext lc = context.LanguageContext; return(StringOps.DoEncode(context, str, errors, lc.GetDefaultEncodingName(), lc.DefaultEncoding, includePreamble: true)); } else { throw PythonOps.TypeError("expected str, got {0}", PythonTypeOps.GetName(obj)); } } else { object?encoder = lookup(context, encoding)[EncoderIndex]; if (!PythonOps.IsCallable(context, encoder)) { throw PythonOps.TypeError("encoding with '{0}' codec failed; encoder must be callable ('{1}' object is not callable)", encoding, PythonTypeOps.GetName(encoder)); } return(PythonOps.GetIndex(context, PythonCalls.Call(context, encoder, obj, errors), 0)); } }
public static object decode(CodeContext /*!*/ context, object?obj, [NotNull, DisallowNull] string?encoding = null, [NotNull] string errors = "strict") { if (encoding == null) { if (obj is IList <byte> bytesLikeObj) { PythonContext lc = context.LanguageContext; return(StringOps.DoDecode(context, bytesLikeObj, errors, lc.GetDefaultEncodingName(), lc.DefaultEncoding)); } else { throw PythonOps.TypeError("expected bytes-like object, got {0}", PythonTypeOps.GetName(obj)); } } else { object?decoder = lookup(context, encoding)[DecoderIndex]; if (!PythonOps.IsCallable(context, decoder)) { throw PythonOps.TypeError("decoding with '{0}' codec failed; decoder must be callable ('{1}' object is not callable)", encoding, PythonTypeOps.GetName(decoder)); } return(PythonOps.GetIndex(context, PythonCalls.Call(context, decoder, obj, errors), 0)); } }
/////////////////////////////////////////////////////////////////////////////////////////////// #region IComparer<string> Members // // NOTE: This comparer tests for matching only. If the text does not match the pattern, a // non-zero value will be returned; however, callers should NOT rely on the exact // non-match value because it is meaningless. // public int Compare( string left, string right ) { bool match = false; Result error = null; if (StringOps.Match( null, mode, left, right, noCase, this, regExOptions, ref match, ref error) == ReturnCode.Ok) { return(ConversionOps.ToInt(!match)); } if (error != null) { throw new ScriptException(error); } else { throw new ScriptException(); } }
private object setdefaultencodingImpl(object name) { if (name == null) { throw Ops.TypeError("name cannot be None"); } string strName = name as string; if (strName == null) { throw Ops.TypeError("name must be a string"); } Encoding enc; if (!StringOps.TryGetEncoding(this, strName, out enc)) { throw Ops.LookupError("'{0}' does not match any available encodings", strName); } DefaultEncoding = enc; __dict__.Remove(SymbolTable.SetDefaultEncoding); return(null); }
private static Tuple <string, int> DoDecode(CodeContext context, string encodingName, Encoding encoding, [BytesConversion] IList <byte> input, string errors, bool final) { var decoded = StringOps.DoDecode(context, input, errors, encodingName, encoding, final, out int numBytes); return(Tuple.Create(decoded, numBytes)); }
public Bytes(CodeContext /*!*/ context, [NotNull] string /*!*/ unicode, [NotNull] string /*!*/ encoding) { _bytes = StringOps.encode(context, unicode, encoding, "strict").MakeByteArray(); }
public override ReturnCode Execute( Interpreter interpreter, IClientData clientData, ArgumentList arguments, ref Result result ) { ReturnCode code = ReturnCode.Ok; if (interpreter != null) { if (arguments != null) { if ((arguments.Count == 2) || (arguments.Count == 3)) { string channelId = arguments[1]; Channel channel = interpreter.GetChannel(channelId, ref result); if (channel != null) { Encoding encoding = null; if (interpreter.GetChannelEncoding(channel, ref encoding) == ReturnCode.Ok) { try { ByteList buffer = null; code = channel.Read(ref buffer, ref result); if (code == ReturnCode.Ok) { string stringValue = null; code = StringOps.GetString( encoding, buffer.ToArray(), EncodingType.Binary, ref stringValue, ref result); if (code == ReturnCode.Ok) { if (arguments.Count == 3) { code = interpreter.SetVariableValue( VariableFlags.None, arguments[2], stringValue, null, ref result); if (code == ReturnCode.Ok) { int length = stringValue.Length; if (length > 0) { result = length; } else { bool canSeek = channel.CanSeek; if ((canSeek && channel.EndOfStream) || (!canSeek && channel.HitEndOfStream)) { result = Channel.EndOfFile; } else { result = length; /* ZERO */ } } } } else { result = stringValue; } } } } catch (Exception e) { Engine.SetExceptionErrorCode(interpreter, e); result = e; code = ReturnCode.Error; } } else { result = String.Format( "failed to get encoding for channel \"{0}\"", channelId); code = ReturnCode.Error; } } else { code = ReturnCode.Error; } } else { result = "wrong # args: should be \"gets channelId ?varName?\""; code = ReturnCode.Error; } } else { result = "invalid argument list"; code = ReturnCode.Error; } } else { result = "invalid interpreter"; code = ReturnCode.Error; } return(code); }
private static PythonTuple CharmapDecodeWorker(CodeContext context, string input, string errors, Encoding e, bool isDecode) { if (input.Length == 0) { return(PythonTuple.MakeTuple(String.Empty, 0)); } string encoding = "charmap"; // default to latin-1 if an encoding is not specified if (e == null) { e = Encoding.GetEncoding("iso-8859-1"); encoding = "latin-1"; } string res = isDecode ? StringOps.DoDecode(context, input, errors, encoding, e) : StringOps.DoEncode(context, input, errors, encoding, e); return(PythonTuple.MakeTuple(res, res.Length)); }
public static object ParseIntegerSign(string text, int b, int start = 0) { int end = text.Length, saveb = b, savestart = start; if (start < 0 || start > end) { throw new ArgumentOutOfRangeException(nameof(start)); } short sign = 1; if (b < 0 || b == 1 || b > 36) { throw new ValueErrorException("base must be >= 2 and <= 36"); } ParseIntegerStart(text, ref b, ref start, end, ref sign); int ret = 0; try { int saveStart = start; for (; ;) { int digit; if (start >= end) { if (saveStart == start) { throw new ValueErrorException(string.Format("invalid literal for int() with base {0}: {1}", b, StringOps.__repr__(text))); } break; } if (!HexValue(text[start], out digit)) { break; } if (!(digit < b)) { if (text[start] == 'l' || text[start] == 'L') { break; } throw new ValueErrorException(string.Format("invalid literal for int() with base {0}: {1}", b, StringOps.__repr__(text))); } checked { // include sign here so that System.Int32.MinValue won't overflow ret = ret * b + sign * digit; } start++; } } catch (OverflowException) { return(ParseBigIntegerSign(text, saveb, savestart)); } ParseIntegerEnd(text, start, end); return(ScriptingRuntimeHelpers.Int32ToObject(ret)); }
public override ReturnCode Execute( Interpreter interpreter, IClientData clientData, ArgumentList arguments, ref Result result ) { ReturnCode code = ReturnCode.Ok; if (interpreter != null) { if (arguments != null) { if (arguments.Count >= 2) { string subCommand = arguments[1]; bool tried = false; code = ScriptOps.TryExecuteSubCommandFromEnsemble( interpreter, this, clientData, arguments, true, false, ref subCommand, ref tried, ref result); if ((code == ReturnCode.Ok) && !tried) { switch (subCommand) { case "decode": { if (arguments.Count >= 3) { OptionDictionary options = new OptionDictionary( new IOption[] { new Option(null, OptionFlags.MustHaveEncodingValue, Index.Invalid, Index.Invalid, "-encoding", null), new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null) }); int argumentIndex = Index.Invalid; code = interpreter.GetOptions(options, arguments, 0, 2, Index.Invalid, true, ref argumentIndex, ref result); if (code == ReturnCode.Ok) { if ((argumentIndex != Index.Invalid) && ((argumentIndex + 1) == arguments.Count)) { Variant value = null; Encoding encoding = null; if (options.IsPresent("-encoding", ref value)) { encoding = (Encoding)value.Value; } if (code == ReturnCode.Ok) { try { string stringValue = null; code = StringOps.GetString(encoding, Convert.FromBase64String(arguments[argumentIndex]), EncodingType.Binary, ref stringValue, ref result); if (code == ReturnCode.Ok) { result = stringValue; } } catch (Exception e) { Engine.SetExceptionErrorCode(interpreter, e); result = e; code = ReturnCode.Error; } } } else { if ((argumentIndex != Index.Invalid) && Option.LooksLikeOption(arguments[argumentIndex])) { result = OptionDictionary.BadOption(options, arguments[argumentIndex]); } else { result = "wrong # args: should be \"base64 decode ?options? string\""; } code = ReturnCode.Error; } } } else { result = "wrong # args: should be \"base64 decode ?options? string\""; code = ReturnCode.Error; } break; } case "encode": { if (arguments.Count >= 3) { OptionDictionary options = new OptionDictionary( new IOption[] { new Option(null, OptionFlags.MustHaveEncodingValue, Index.Invalid, Index.Invalid, "-encoding", null), new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null) }); int argumentIndex = Index.Invalid; code = interpreter.GetOptions(options, arguments, 0, 2, Index.Invalid, true, ref argumentIndex, ref result); if (code == ReturnCode.Ok) { if ((argumentIndex != Index.Invalid) && ((argumentIndex + 1) == arguments.Count)) { Variant value = null; Encoding encoding = null; if (options.IsPresent("-encoding", ref value)) { encoding = (Encoding)value.Value; } if (code == ReturnCode.Ok) { try { byte[] bytes = null; code = StringOps.GetBytes( encoding, arguments[argumentIndex], EncodingType.Binary, ref bytes, ref result); if (code == ReturnCode.Ok) { result = Convert.ToBase64String(bytes, Base64FormattingOptions.InsertLineBreaks); } } catch (Exception e) { Engine.SetExceptionErrorCode(interpreter, e); result = e; code = ReturnCode.Error; } } } else { if ((argumentIndex != Index.Invalid) && Option.LooksLikeOption(arguments[argumentIndex])) { result = OptionDictionary.BadOption(options, arguments[argumentIndex]); } else { result = "wrong # args: should be \"base64 encode ?options? string\""; } code = ReturnCode.Error; } } } else { result = "wrong # args: should be \"base64 encode ?options? string\""; code = ReturnCode.Error; } break; } default: { result = ScriptOps.BadSubCommand( interpreter, null, null, subCommand, this, null, null); code = ReturnCode.Error; break; } } } } else { result = "wrong # args: should be \"base64 option ?arg ...?\""; code = ReturnCode.Error; } } else { result = "invalid argument list"; code = ReturnCode.Error; } } else { result = "invalid interpreter"; code = ReturnCode.Error; } return(code); }
public SimpleType(CodeContext /*!*/ context, string name, PythonTuple bases, PythonDictionary dict) : base(context, name, bases, dict) { string sVal; const string allowedTypes = "?cbBghHiIlLdfuzZqQPXOv"; const string swappedTypes = "fdhHiIlLqQ"; if (!TryGetBoundCustomMember(context, "_type_", out object val) || (sVal = StringOps.AsString(val)) == null || sVal.Length != 1 || allowedTypes.IndexOf(sVal[0]) == -1) { throw PythonOps.AttributeError("AttributeError: class must define a '_type_' attribute which must be a single character string containing one of '{0}'.", allowedTypes); } _charType = sVal[0]; switch (_charType) { case '?': _type = SimpleTypeKind.Boolean; break; case 'c': _type = SimpleTypeKind.Char; break; case 'b': _type = SimpleTypeKind.SignedByte; break; case 'B': _type = SimpleTypeKind.UnsignedByte; break; case 'h': _type = SimpleTypeKind.SignedShort; break; case 'H': _type = SimpleTypeKind.UnsignedShort; break; case 'i': _type = SimpleTypeKind.SignedInt; break; case 'I': _type = SimpleTypeKind.UnsignedInt; break; case 'l': _type = SimpleTypeKind.SignedLong; break; case 'L': _type = SimpleTypeKind.UnsignedLong; break; case 'f': _type = SimpleTypeKind.Single; break; case 'g': // long double, new in 2.6 case 'd': _type = SimpleTypeKind.Double; break; case 'q': _type = SimpleTypeKind.SignedLongLong; break; case 'Q': _type = SimpleTypeKind.UnsignedLongLong; break; case 'O': _type = SimpleTypeKind.Object; break; case 'P': _type = SimpleTypeKind.Pointer; break; case 'z': _type = SimpleTypeKind.CharPointer; break; case 'Z': _type = SimpleTypeKind.WCharPointer; break; case 'u': _type = SimpleTypeKind.WChar; break; case 'v': _type = SimpleTypeKind.VariantBool; break; case 'X': _type = SimpleTypeKind.BStr; break; default: throw new NotImplementedException("simple type " + sVal); } if (!name.EndsWith("_be", StringComparison.Ordinal) && !name.EndsWith("_le", StringComparison.Ordinal) && swappedTypes.IndexOf(_charType) != -1) { CreateSwappedType(context, name, bases, dict); } _format = (BitConverter.IsLittleEndian ? '<' : '>') + _charType.ToString(); }
/// <summary> /// Interrogates the importing module for __name__ and __path__, which determine /// whether the imported module (whose name is 'name') is being imported as nested /// module (__path__ is present) or as sibling. /// /// For sibling import, the full name of the imported module is parent.sibling /// For nested import, the full name of the imported module is parent.module.nested /// where parent.module is the mod.__name__ /// </summary> /// <param name="context"></param> /// <param name="globals">the globals dictionary</param> /// <param name="name">Name of the module to be imported</param> /// <param name="full">Output - full name of the module being imported</param> /// <param name="path">Path to use to search for "full"</param> /// <param name="level">the import level for relaive imports</param> /// <param name="parentMod">the parent module</param> /// <param name="package">the global __package__ value</param> /// <returns></returns> private static bool TryGetNameAndPath(CodeContext /*!*/ context, object globals, string name, int level, string package, out string full, out List path, out PythonModule parentMod) { Debug.Assert(level != 0); // shouldn't be here for absolute imports // Unless we can find enough information to perform relative import, // we are going to import the module whose name we got full = name; path = null; parentMod = null; // We need to get __name__ to find the name of the imported module. // If absent, fall back to absolute import object attribute; if (!(globals is PythonDictionary pyGlobals) || !pyGlobals._storage.TryGetName(out attribute)) { return(false); } // And the __name__ needs to be string if (!(attribute is string modName)) { return(false); } string pn; if (package == null) { // If the module has __path__ (and __path__ is list), nested module is being imported // otherwise, importing sibling to the importing module if (pyGlobals._storage.TryGetPath(out attribute) && (path = attribute as List) != null) { // found __path__, importing nested module. The actual name of the nested module // is the name of the mod plus the name of the imported module if (level == -1) { // absolute import of some module full = modName + "." + name; object parentModule; if (context.LanguageContext.SystemStateModules.TryGetValue(modName, out parentModule)) { parentMod = parentModule as PythonModule; } } else if (String.IsNullOrEmpty(name)) { // relative import of ancestor full = (StringOps.rsplit(modName, ".", level - 1)[0] as string); } else { // relative import of some ancestors child string parentName = (StringOps.rsplit(modName, ".", level - 1)[0] as string); full = parentName + "." + name; object parentModule; if (context.LanguageContext.SystemStateModules.TryGetValue(parentName, out parentModule)) { parentMod = parentModule as PythonModule; } } return(true); } // importing sibling. The name of the imported module replaces // the last element in the importing module name int lastDot = modName.LastIndexOf('.'); if (lastDot == -1) { // name doesn't include dot, only absolute import possible if (level > 0) { throw PythonOps.ValueError("Attempted relative import in non-package"); } return(false); } // need to remove more than one name int tmpLevel = level; while (tmpLevel > 1 && lastDot != -1) { lastDot = modName.LastIndexOf('.', lastDot - 1); tmpLevel--; } if (lastDot == -1) { pn = modName; } else { pn = modName.Substring(0, lastDot); } } else { // __package__ doesn't include module name, so level is - 1. pn = GetParentPackageName(level - 1, package.Split('.')); } path = GetParentPathAndModule(context, pn, out parentMod); if (path != null) { if (String.IsNullOrEmpty(name)) { full = pn; } else { full = pn + "." + name; } return(true); } if (level > 0) { throw PythonOps.SystemError("Parent module '{0}' not loaded, cannot perform relative import", pn); } // not enough information - absolute import return(false); }
public Bytes(CodeContext context, [NotNull] string unicode, [NotNull] string encoding) { _bytes = StringOps.encode(context, unicode, encoding, "strict").UnsafeByteArray; }
public void __init__(CodeContext /*!*/ context, string source, string encoding, [DefaultParameterValue("strict")] string errors) { _bytes = new List <byte>(StringOps.encode(context, source, encoding, errors).MakeByteArray()); }
/////////////////////////////////////////////////////////////////////// #region Private Static Methods private static StringBuilder NewStringBuilder( int capacity /* in */ ) { return(StringOps.NewStringBuilder(capacity)); }
public static BigInteger ParseBigIntegerSign(string text, int b, int start = 0) { int end = text.Length; if (start < 0 || start > end) { throw new ArgumentOutOfRangeException(nameof(start)); } short sign = 1; if (b < 0 || b == 1 || b > 36) { throw new ValueErrorException("base must be >= 2 and <= 36"); } ParseIntegerStart(text, ref b, ref start, end, ref sign); BigInteger ret = BigInteger.Zero; int saveStart = start; for (; ;) { int digit; if (start >= end) { if (start == saveStart) { throw new ValueErrorException(string.Format("invalid literal for long() with base {0}: {1}", b, StringOps.__repr__(text))); } break; } if (!HexValue(text[start], out digit)) { break; } if (!(digit < b)) { if (text[start] == 'l' || text[start] == 'L') { break; } throw new ValueErrorException(string.Format("invalid literal for long() with base {0}: {1}", b, StringOps.__repr__(text))); } ret = ret * b + digit; start++; } if (start < end && (text[start] == 'l' || text[start] == 'L')) { start++; } ParseIntegerEnd(text, start, end); return(sign < 0 ? -ret : ret); }
// 3.0-only public static object IterMethodForString(string self) { return(StringOps.StringEnumerator(self)); }
private object StringModulo(CallSite site, string self, object other) { return(StringOps.Mod(Context.SharedContext, self, other)); }
public string decode(CodeContext /*!*/ context, [Optional] object /*!*/ encoding, [NotNull] string /*!*/ errors = "strict") { return(StringOps.decode(context, _bytes.MakeString(), encoding, errors)); }
internal override ConstantExpression ConstantFold() { Expression left = _left.ConstantFold(); Expression right = _right.ConstantFold(); var constLeft = left as ConstantExpression; var constRight = right as ConstantExpression; try { if (constLeft != null && constRight != null && constLeft.Value != null && constRight.Value != null && constLeft.Value.GetType() == constRight.Value.GetType()) { #region Generated Python Constant Folding // *** BEGIN GENERATED CODE *** // generated by function: gen_constant_folding from: generate_ops.py if (constLeft.Value.GetType() == typeof(Int32)) { switch (_op) { case PythonOperator.Add: return(new ConstantExpression(Int32Ops.Add((Int32)constLeft.Value, (Int32)constRight.Value))); case PythonOperator.Subtract: return(new ConstantExpression(Int32Ops.Subtract((Int32)constLeft.Value, (Int32)constRight.Value))); case PythonOperator.Power: return(new ConstantExpression(Int32Ops.Power((Int32)constLeft.Value, (Int32)constRight.Value))); case PythonOperator.Multiply: return(new ConstantExpression(Int32Ops.Multiply((Int32)constLeft.Value, (Int32)constRight.Value))); case PythonOperator.FloorDivide: return(new ConstantExpression(Int32Ops.FloorDivide((Int32)constLeft.Value, (Int32)constRight.Value))); case PythonOperator.Divide: return(new ConstantExpression(Int32Ops.Divide((Int32)constLeft.Value, (Int32)constRight.Value))); case PythonOperator.TrueDivide: return(new ConstantExpression(Int32Ops.TrueDivide((Int32)constLeft.Value, (Int32)constRight.Value))); case PythonOperator.Mod: return(new ConstantExpression(Int32Ops.Mod((Int32)constLeft.Value, (Int32)constRight.Value))); case PythonOperator.LeftShift: return(new ConstantExpression(Int32Ops.LeftShift((Int32)constLeft.Value, (Int32)constRight.Value))); case PythonOperator.RightShift: return(new ConstantExpression(Int32Ops.RightShift((Int32)constLeft.Value, (Int32)constRight.Value))); case PythonOperator.BitwiseAnd: return(new ConstantExpression(Int32Ops.BitwiseAnd((Int32)constLeft.Value, (Int32)constRight.Value))); case PythonOperator.BitwiseOr: return(new ConstantExpression(Int32Ops.BitwiseOr((Int32)constLeft.Value, (Int32)constRight.Value))); case PythonOperator.ExclusiveOr: return(new ConstantExpression(Int32Ops.ExclusiveOr((Int32)constLeft.Value, (Int32)constRight.Value))); case PythonOperator.LessThan: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(Int32Ops.Compare((Int32)constLeft.Value, (Int32)constRight.Value) < 0))); case PythonOperator.GreaterThan: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(Int32Ops.Compare((Int32)constLeft.Value, (Int32)constRight.Value) > 0))); case PythonOperator.LessThanOrEqual: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(Int32Ops.Compare((Int32)constLeft.Value, (Int32)constRight.Value) <= 0))); case PythonOperator.GreaterThanOrEqual: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(Int32Ops.Compare((Int32)constLeft.Value, (Int32)constRight.Value) >= 0))); case PythonOperator.Equals: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(Int32Ops.Compare((Int32)constLeft.Value, (Int32)constRight.Value) == 0))); case PythonOperator.NotEquals: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(Int32Ops.Compare((Int32)constLeft.Value, (Int32)constRight.Value) != 0))); } } if (constLeft.Value.GetType() == typeof(Double)) { switch (_op) { case PythonOperator.Add: return(new ConstantExpression(DoubleOps.Add((Double)constLeft.Value, (Double)constRight.Value))); case PythonOperator.Subtract: return(new ConstantExpression(DoubleOps.Subtract((Double)constLeft.Value, (Double)constRight.Value))); case PythonOperator.Power: return(new ConstantExpression(DoubleOps.Power((Double)constLeft.Value, (Double)constRight.Value))); case PythonOperator.Multiply: return(new ConstantExpression(DoubleOps.Multiply((Double)constLeft.Value, (Double)constRight.Value))); case PythonOperator.FloorDivide: return(new ConstantExpression(DoubleOps.FloorDivide((Double)constLeft.Value, (Double)constRight.Value))); case PythonOperator.Divide: return(new ConstantExpression(DoubleOps.Divide((Double)constLeft.Value, (Double)constRight.Value))); case PythonOperator.TrueDivide: return(new ConstantExpression(DoubleOps.TrueDivide((Double)constLeft.Value, (Double)constRight.Value))); case PythonOperator.Mod: return(new ConstantExpression(DoubleOps.Mod((Double)constLeft.Value, (Double)constRight.Value))); case PythonOperator.LessThan: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(DoubleOps.Compare((Double)constLeft.Value, (Double)constRight.Value) < 0))); case PythonOperator.GreaterThan: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(DoubleOps.Compare((Double)constLeft.Value, (Double)constRight.Value) > 0))); case PythonOperator.LessThanOrEqual: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(DoubleOps.Compare((Double)constLeft.Value, (Double)constRight.Value) <= 0))); case PythonOperator.GreaterThanOrEqual: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(DoubleOps.Compare((Double)constLeft.Value, (Double)constRight.Value) >= 0))); case PythonOperator.Equals: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(DoubleOps.Compare((Double)constLeft.Value, (Double)constRight.Value) == 0))); case PythonOperator.NotEquals: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(DoubleOps.Compare((Double)constLeft.Value, (Double)constRight.Value) != 0))); } } if (constLeft.Value.GetType() == typeof(BigInteger)) { switch (_op) { case PythonOperator.Add: return(new ConstantExpression(BigIntegerOps.Add((BigInteger)constLeft.Value, (BigInteger)constRight.Value))); case PythonOperator.Subtract: return(new ConstantExpression(BigIntegerOps.Subtract((BigInteger)constLeft.Value, (BigInteger)constRight.Value))); case PythonOperator.Power: return(new ConstantExpression(BigIntegerOps.Power((BigInteger)constLeft.Value, (BigInteger)constRight.Value))); case PythonOperator.Multiply: return(new ConstantExpression(BigIntegerOps.Multiply((BigInteger)constLeft.Value, (BigInteger)constRight.Value))); case PythonOperator.FloorDivide: return(new ConstantExpression(BigIntegerOps.FloorDivide((BigInteger)constLeft.Value, (BigInteger)constRight.Value))); case PythonOperator.Divide: return(new ConstantExpression(BigIntegerOps.Divide((BigInteger)constLeft.Value, (BigInteger)constRight.Value))); case PythonOperator.TrueDivide: return(new ConstantExpression(BigIntegerOps.TrueDivide((BigInteger)constLeft.Value, (BigInteger)constRight.Value))); case PythonOperator.Mod: return(new ConstantExpression(BigIntegerOps.Mod((BigInteger)constLeft.Value, (BigInteger)constRight.Value))); case PythonOperator.LeftShift: return(new ConstantExpression(BigIntegerOps.LeftShift((BigInteger)constLeft.Value, (BigInteger)constRight.Value))); case PythonOperator.RightShift: return(new ConstantExpression(BigIntegerOps.RightShift((BigInteger)constLeft.Value, (BigInteger)constRight.Value))); case PythonOperator.BitwiseAnd: return(new ConstantExpression(BigIntegerOps.BitwiseAnd((BigInteger)constLeft.Value, (BigInteger)constRight.Value))); case PythonOperator.BitwiseOr: return(new ConstantExpression(BigIntegerOps.BitwiseOr((BigInteger)constLeft.Value, (BigInteger)constRight.Value))); case PythonOperator.ExclusiveOr: return(new ConstantExpression(BigIntegerOps.ExclusiveOr((BigInteger)constLeft.Value, (BigInteger)constRight.Value))); case PythonOperator.LessThan: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(BigIntegerOps.Compare((BigInteger)constLeft.Value, (BigInteger)constRight.Value) < 0))); case PythonOperator.GreaterThan: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(BigIntegerOps.Compare((BigInteger)constLeft.Value, (BigInteger)constRight.Value) > 0))); case PythonOperator.LessThanOrEqual: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(BigIntegerOps.Compare((BigInteger)constLeft.Value, (BigInteger)constRight.Value) <= 0))); case PythonOperator.GreaterThanOrEqual: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(BigIntegerOps.Compare((BigInteger)constLeft.Value, (BigInteger)constRight.Value) >= 0))); case PythonOperator.Equals: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(BigIntegerOps.Compare((BigInteger)constLeft.Value, (BigInteger)constRight.Value) == 0))); case PythonOperator.NotEquals: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(BigIntegerOps.Compare((BigInteger)constLeft.Value, (BigInteger)constRight.Value) != 0))); } } if (constLeft.Value.GetType() == typeof(Complex)) { switch (_op) { case PythonOperator.Add: return(new ConstantExpression(ComplexOps.Add((Complex)constLeft.Value, (Complex)constRight.Value))); case PythonOperator.Subtract: return(new ConstantExpression(ComplexOps.Subtract((Complex)constLeft.Value, (Complex)constRight.Value))); case PythonOperator.Power: return(new ConstantExpression(ComplexOps.Power((Complex)constLeft.Value, (Complex)constRight.Value))); case PythonOperator.Multiply: return(new ConstantExpression(ComplexOps.Multiply((Complex)constLeft.Value, (Complex)constRight.Value))); case PythonOperator.Divide: return(new ConstantExpression(ComplexOps.Divide((Complex)constLeft.Value, (Complex)constRight.Value))); case PythonOperator.TrueDivide: return(new ConstantExpression(ComplexOps.TrueDivide((Complex)constLeft.Value, (Complex)constRight.Value))); } } // *** END GENERATED CODE *** #endregion if (constLeft.Value.GetType() == typeof(string) && _op == PythonOperator.Add) { return(new ConstantExpression((string)constLeft.Value + (string)constRight.Value)); } } else if (_op == PythonOperator.Multiply && constLeft != null && constRight != null) { // 10000 check is to avoid creating massive strings in IL - there's a limit to the number of // bytes of strings we get to have so we don't want to use them all up. if (constLeft.Value.GetType() == typeof(string) && constRight.Value.GetType() == typeof(int)) { var res = StringOps.Multiply((string)constLeft.Value, (int)constRight.Value); if (res.Length < MaximumInlineStringLength) { return(new ConstantExpression(res)); } } else if (constLeft.Value.GetType() == typeof(int) && constRight.Value.GetType() == typeof(string)) { var res = StringOps.Multiply((string)constRight.Value, (int)constLeft.Value); if (res.Length < MaximumInlineStringLength) { return(new ConstantExpression(res)); } } } } catch (ArithmeticException) { } return(null); }
public Bytes(CodeContext context, [NotNull] string @string, [NotNull] string encoding, [NotNull] string errors) { _bytes = StringOps.encode(context, @string, encoding, errors).UnsafeByteArray; }
public string decode(CodeContext /*!*/ context, [Optional] object /*!*/ encoding, [DefaultParameterValue("strict")][NotNull] string /*!*/ errors) { return(StringOps.decode(context, _bytes.MakeString(), encoding, errors)); }
public SimpleType(CodeContext /*!*/ context, string name, PythonTuple bases, PythonDictionary dict) : base(context, name, bases, dict) { object val; string sVal; const string allowedTypes = "?cbBghHiIlLdfuzZqQPXOv"; if (!TryGetBoundCustomMember(context, "_type_", out val) || (sVal = StringOps.AsString(val)) == null || sVal.Length != 1 || allowedTypes.IndexOf(sVal[0]) == -1) { throw PythonOps.AttributeError("AttributeError: class must define a '_type_' attribute which must be a single character string containing one of '{0}'.", allowedTypes); } _charType = sVal[0]; switch (sVal[0]) { case '?': _type = SimpleTypeKind.Boolean; break; case 'c': _type = SimpleTypeKind.Char; break; case 'b': _type = SimpleTypeKind.SignedByte; break; case 'B': _type = SimpleTypeKind.UnsignedByte; break; case 'h': _type = SimpleTypeKind.SignedShort; break; case 'H': _type = SimpleTypeKind.UnsignedShort; break; case 'i': _type = SimpleTypeKind.SignedInt; break; case 'I': _type = SimpleTypeKind.UnsignedInt; break; case 'l': _type = SimpleTypeKind.SignedLong; break; case 'L': _type = SimpleTypeKind.UnsignedLong; break; case 'f': _type = SimpleTypeKind.Single; break; case 'g': // long double, new in 2.6 case 'd': _type = SimpleTypeKind.Double; break; case 'q': _type = SimpleTypeKind.SignedLongLong; break; case 'Q': _type = SimpleTypeKind.UnsignedLongLong; break; case 'O': _type = SimpleTypeKind.Object; break; case 'P': _type = SimpleTypeKind.Pointer; break; case 'z': _type = SimpleTypeKind.CharPointer; break; case 'Z': _type = SimpleTypeKind.WCharPointer; break; case 'u': _type = SimpleTypeKind.WChar; break; case 'v': _type = SimpleTypeKind.VariantBool; break; case 'X': throw new NotImplementedException("simple type " + sVal); } }
public static PythonTuple utf_8_decode(CodeContext context, [BytesConversion] IList <byte> input, string errors = "strict", bool final = false) { StringOps.TryGetEncoding("utf-8", out Encoding encoding); // no preamble skipping return(DoDecode(context, "utf-8", encoding, input, errors, final).ToPythonTuple()); }
public override void ProcessNode(BslFunctionCallNode node) { ExpressionReference expressionReference = m_decompilerLookup.FunctionOpCodeLookup[node.OperationCode]; FunctionReference functionReference; OperatorReference operatorReference; GlobalReference globalReference; if ((functionReference = expressionReference as FunctionReference) != null) { if (functionReference.Function.IsOverloadedByBooleanArgument) { node.Children.Add(new BoolValueNode(functionReference.Overload == 1)); } node.Replace(new PslFunctionUsageNode(functionReference.Function, node.Children)); foreach (NodeBase child in node.Children) { child.VisitThis(this); } } else if ((operatorReference = expressionReference as OperatorReference) != null) { switch (operatorReference.Operation.Specification) { case FunctionSpecification.Begin: node.Replace(new GroupingNode(node.Children)); foreach (NodeBase child in node.Children) { child.VisitThis(this); } break; case FunctionSpecification.BeginRandom: ValueDiscrepancy vd = SingleValueDiscrepancySearch.Perform(node.Children); if (vd != null) { short typeIndex = vd.TypeIndex; string pluralTypeName = m_engineDefinition.GetTypeName(typeIndex); List <NodeBase> randomizerExpressions = node.Children[0].Children; VariableNode iteratorParameter = new VariableNode(pluralTypeName + "s", typeIndex, true, true, null); vd.Nodes[0].Replace(new VariableReferenceValueNode(iteratorParameter, typeIndex)); List <NodeBase> parameters = new List <NodeBase>(); parameters.Add(iteratorParameter); ScriptNode randomFunction = new ScriptNode("Randomize" + StringOps.CapitalizeWords(pluralTypeName), ScriptType.Random, (short)IntegralValueType.Void, randomizerExpressions); m_scriptInsertionQueue.QueueInsertion(m_state.CurrentScriptIndex, randomFunction); List <NodeBase> randomFunctionArguments = new List <NodeBase>(); randomFunctionArguments.Add(new PslArrayNode(typeIndex, vd.Nodes)); node.Replace(new ScriptCallNode(randomFunction, randomFunctionArguments)); foreach (NodeBase child in node.Children) { child.VisitThis(this); } } break; case FunctionSpecification.If: // Early recursion to identify GroupingNodes and sub Ifs before constructing conditional node foreach (NodeBase child in node.Children) { child.VisitThis(this); } ConditionalConstructNode conditionalConstruct = null; NodeBase condition = node.Children[0]; GroupingNode expressions = GroupingNode.MakeGrouping(node.Children[1]); GroupingNode elseExpressions = null; if (node.Children.Count > 2) { elseExpressions = GroupingNode.MakeGrouping(node.Children[2]); ConditionalConstructNode embeddedIf; if (elseExpressions.Children.Count == 1 && (embeddedIf = elseExpressions.Children[0] as ConditionalConstructNode) != null) { BoolValueNode potentialAlwaysTrueStatement = embeddedIf.Conditions[0] as BoolValueNode; if (potentialAlwaysTrueStatement != null && potentialAlwaysTrueStatement.Value) { elseExpressions = embeddedIf.ExpressionSets[0] as GroupingNode; } else { conditionalConstruct = embeddedIf; conditionalConstruct.InsertConditional(condition, expressions); } } } if (conditionalConstruct == null) { List <NodeBase> conditions = new List <NodeBase>(); conditions.Add(condition); List <NodeBase> expressionSets = new List <NodeBase>(); expressionSets.Add(expressions); conditionalConstruct = new ConditionalConstructNode(conditions, expressionSets, elseExpressions); } node.Replace(conditionalConstruct); break; case FunctionSpecification.Cond: throw new NeedMoreResearchException("How does cond look in compiled form - how are its children mapped out?"); //node.Replace(new ConditionalConstructNode()); //break; default: List <NodeBase> children = node.Children; node.Replace(new PslOperatorUsageNode(operatorReference.Operation, children)); foreach (NodeBase child in children) { child.VisitThis(this); } break; } } else if ((globalReference = expressionReference as GlobalReference) != null) { switch (globalReference.Method) { case GlobalReference.AccessMethod.Get: node.Replace(new PslGameGlobalReferenceNode(globalReference.Global)); break; case GlobalReference.AccessMethod.Set: List <NodeBase> operands = new List <NodeBase>(); operands.Add(new PslGameGlobalReferenceNode(globalReference.Global)); operands.Add(node.Children[0]); node.Replace(new PslOperatorUsageNode(m_engineDefinition.SpecificFunctions[(int)FunctionSpecification.Set], operands)); operands[1].VisitThis(this); break; } } else if (expressionReference is CasterReference) { node.Replace(node.Children[0]); node.Children[0].VisitThis(this); } }
private static Tuple <Bytes, int> DoEncode(CodeContext context, string encodingName, Encoding encoding, string input, string errors, bool includePreamble = false) { var res = StringOps.DoEncode(context, input, errors, encodingName, encoding, includePreamble); return(Tuple.Create(res, input.Length)); }
public static PythonTuple escape_decode(CodeContext /*!*/ context, string data, string errors = "strict") => escape_decode(StringOps.DoEncodeUtf8(context, data), errors);