Newer
Older
dub_jkp / source / dub / internal / sdlang / util.d
@Sebastian Wilzbach Sebastian Wilzbach on 23 Feb 2017 2 KB Remove all trailing whitespace
  1. // SDLang-D
  2. // Written in the D programming language.
  3.  
  4. module dub.internal.sdlang.util;
  5.  
  6. version (Have_sdlang_d) public import sdlang.util;
  7. else:
  8.  
  9. import std.algorithm;
  10. import std.datetime;
  11. import std.stdio;
  12. import std.string;
  13.  
  14. import dub.internal.sdlang.token;
  15.  
  16. enum sdlangVersion = "0.9.1";
  17.  
  18. alias immutable(ubyte)[] ByteString;
  19.  
  20. auto startsWith(T)(string haystack, T needle)
  21. if( is(T:ByteString) || is(T:string) )
  22. {
  23. return std.algorithm.startsWith( cast(ByteString)haystack, cast(ByteString)needle );
  24. }
  25.  
  26. struct Location
  27. {
  28. string file; /// Filename (including path)
  29. int line; /// Zero-indexed
  30. int col; /// Zero-indexed, Tab counts as 1
  31. size_t index; /// Index into the source
  32.  
  33. this(int line, int col, int index)
  34. {
  35. this.line = line;
  36. this.col = col;
  37. this.index = index;
  38. }
  39.  
  40. this(string file, int line, int col, int index)
  41. {
  42. this.file = file;
  43. this.line = line;
  44. this.col = col;
  45. this.index = index;
  46. }
  47.  
  48. string toString()
  49. {
  50. return "%s(%s:%s)".format(file, line+1, col+1);
  51. }
  52. }
  53.  
  54. void removeIndex(E)(ref E[] arr, ptrdiff_t index)
  55. {
  56. arr = arr[0..index] ~ arr[index+1..$];
  57. }
  58.  
  59. void trace(string file=__FILE__, size_t line=__LINE__, TArgs...)(TArgs args)
  60. {
  61. version(sdlangTrace)
  62. {
  63. writeln(file, "(", line, "): ", args);
  64. stdout.flush();
  65. }
  66. }
  67.  
  68. string toString(TypeInfo ti)
  69. {
  70. if (ti == typeid( bool )) return "bool";
  71. else if(ti == typeid( string )) return "string";
  72. else if(ti == typeid( dchar )) return "dchar";
  73. else if(ti == typeid( int )) return "int";
  74. else if(ti == typeid( long )) return "long";
  75. else if(ti == typeid( float )) return "float";
  76. else if(ti == typeid( double )) return "double";
  77. else if(ti == typeid( real )) return "real";
  78. else if(ti == typeid( Date )) return "Date";
  79. else if(ti == typeid( DateTimeFrac )) return "DateTimeFrac";
  80. else if(ti == typeid( DateTimeFracUnknownZone )) return "DateTimeFracUnknownZone";
  81. else if(ti == typeid( SysTime )) return "SysTime";
  82. else if(ti == typeid( Duration )) return "Duration";
  83. else if(ti == typeid( ubyte[] )) return "ubyte[]";
  84. else if(ti == typeid( typeof(null) )) return "null";
  85.  
  86. return "{unknown}";
  87. }
  88.  
  89. enum BOM {
  90. UTF8, /// UTF-8
  91. UTF16LE, /// UTF-16 (little-endian)
  92. UTF16BE, /// UTF-16 (big-endian)
  93. UTF32LE, /// UTF-32 (little-endian)
  94. UTF32BE, /// UTF-32 (big-endian)
  95. }
  96.  
  97. enum NBOM = __traits(allMembers, BOM).length;
  98. immutable ubyte[][NBOM] ByteOrderMarks =
  99. [
  100. [0xEF, 0xBB, 0xBF], //UTF8
  101. [0xFF, 0xFE], //UTF16LE
  102. [0xFE, 0xFF], //UTF16BE
  103. [0xFF, 0xFE, 0x00, 0x00], //UTF32LE
  104. [0x00, 0x00, 0xFE, 0xFF] //UTF32BE
  105. ];