stringio.monkey2 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. Namespace std.stringio
  2. Using libc
  3. 'These will eventually be string extensions, eg: Function String.Load() and Method String.Save()
  4. #rem monkeydoc Loads a utf8 encoded string from a file.
  5. An empty string will be returned if the file could not be opened.
  6. @param path The path of the file.
  7. @return A String containing the contents of the file.
  8. #end
  9. Function LoadString:String( path:String )
  10. Local data:=DataBuffer.Load( path )
  11. If Not data Return ""
  12. Local str:=String.FromUtf8String( data.Data,data.Length )
  13. data.Discard()
  14. Return str
  15. End
  16. #rem monkeydoc Saves a string to a file in utf8 encoding.
  17. @param path The path of the file.
  18. @param str The string to save.
  19. @return False if the file could not be opened.
  20. #end
  21. Function SaveString:Bool( str:String,path:String )
  22. Local data:=New DataBuffer( str.Utf8Length )
  23. str.ToUtf8String( data.Data,data.Length )
  24. Local ok:=data.Save( path )
  25. data.Discard()
  26. Return ok
  27. End
  28. #rem monkeydoc @hidden Use ULongToString
  29. #end
  30. Function Hex:String( value:ULong )
  31. Local str:=""
  32. While value
  33. Local nyb:=value & $f
  34. If nyb<10 str=String.FromChar( nyb+48 )+str Else str=String.FromChar( nyb+55 )+str
  35. value=value Shr 4
  36. Wend
  37. Return str ? str Else "0"
  38. End
  39. #rem monkeydoc @hidden Use StringToULong
  40. #end
  41. Function FromHex:ULong( hex:String )
  42. Local value:ULong
  43. For Local i:=0 Until hex.Length
  44. Local ch:=hex[i]
  45. If ch>=48 And ch<58
  46. value=value Shl 4 | (ch-48)
  47. Else If ch>=65 And ch<71
  48. value=value Shl 4 | (ch-55)
  49. Else If ch>=97 And ch<103
  50. value=value Shl 4 | (ch-87)
  51. Else
  52. Exit
  53. Endif
  54. Next
  55. Return value
  56. End
  57. #rem monkeydoc Converts an unsigned long value to a string.
  58. @param value Value to convert.
  59. @param base Numeric base for conversion, eg: 2 for binary, 16 for hex etc.
  60. #end
  61. Function ULongToString:String( value:ULong,base:UInt )
  62. Local str:=""
  63. While value
  64. Local n:=value Mod base
  65. If n<10 str=String.FromChar( n+48 )+str Else str=String.FromChar( n+55 )+str
  66. value/=base
  67. Wend
  68. Return str
  69. End
  70. #rem monkeydoc Converts a string to an unsigned long value.
  71. @param str String to convert.
  72. @param base Numeric base for conversion, eg: 2 for binary, 16 for hex etc.
  73. #end
  74. Function StringToULong:ULong( str:String,base:UInt )
  75. Local value:ULong
  76. If base<=10
  77. For Local ch:=Eachin str
  78. If ch>=48 And ch<48+base value=value*base+(ch-48) Else Exit
  79. Next
  80. Return value
  81. Endif
  82. For Local ch:=Eachin str
  83. If ch>=48 And ch<58
  84. value=value*base+(ch-48)
  85. Else If ch>=65 And ch<65+(base-10)
  86. value=value*base+(ch-55)
  87. Else If ch>=97 And ch<97+(base-10)
  88. value=value*base+(ch-87)
  89. Else
  90. Exit
  91. Endif
  92. Next
  93. Return value
  94. End