PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/Languages/IronPython/IronPython/Runtime/Operations/ListOfTOps.cs

http://github.com/IronLanguages/main
C# | 57 lines | 37 code | 6 blank | 14 comment | 4 complexity | a152846074c7658498d7fae1f8934319 MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Apache License, Version 2.0, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System.Collections.Generic;
  16. using System.Text;
  17. using Microsoft.Scripting.Runtime;
  18. using IronPython.Runtime.Types;
  19. namespace IronPython.Runtime.Operations {
  20. public static class ListOfTOps<T> {
  21. public static string __repr__(CodeContext/*!*/ context, List<T> self) {
  22. List<object> infinite = PythonOps.GetAndCheckInfinite(self);
  23. if (infinite == null) {
  24. return "[...]";
  25. }
  26. int index = infinite.Count;
  27. infinite.Add(self);
  28. try {
  29. StringBuilder res = new StringBuilder();
  30. res.Append("List[");
  31. res.Append(DynamicHelpers.GetPythonTypeFromType(typeof(T)).Name);
  32. res.Append("](");
  33. if (self.Count > 0) {
  34. res.Append("[");
  35. string comma = "";
  36. foreach (T obj in self) {
  37. res.Append(comma);
  38. res.Append(PythonOps.Repr(context, obj));
  39. comma = ", ";
  40. }
  41. res.Append("]");
  42. }
  43. res.Append(")");
  44. return res.ToString();
  45. } finally {
  46. System.Diagnostics.Debug.Assert(index == infinite.Count - 1);
  47. infinite.RemoveAt(index);
  48. }
  49. }
  50. }
  51. }