stringio.monkey2 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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.FromUtf8( 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.ToUtf8( data.Data,data.Length )
  24. Local ok:=data.Save( path )
  25. data.Discard()
  26. Return ok
  27. End
  28. #rem monkeydoc Converts a ulong value to a hexadecimal string.
  29. @param value The value to convert.
  30. @return The hexadecimal representation of `value`.
  31. #end
  32. Function Hex:String( value:ULong )
  33. Local str:=""
  34. While value
  35. Local nyb:=value & $f
  36. If nyb<10 str=String.FromChar( nyb+48 )+str Else str=String.FromChar( nyb+55 )+str
  37. value=value Shr 4
  38. Wend
  39. Return str ? str Else "0"
  40. End