Strings.txt 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <- Manual.html | Back to main page
  2. Title: Basic text operations
  3. The string API can be found in Source/DFPSR/api/stringAPI.h, where you can read the specific documentation for each method.
  4. The methods allow easily loading and parsing files using a single page of code without the risk of corrupting memory.
  5. Element access it read-only even for writable strings, so you're supposed to create strings by clearing and appending.
  6. This could probably be done with a single string type, but it's easier to reason about using one that heap allocates (String) and one that's lighter (ReadableString).
  7. ---
  8. Title2: Encoding
  9. Both dsr::String and dsr::ReadableString are encoded in the UTF-32 format
  10. using only line-feed for line-breaks.
  11. This takes more memory but guarantees that each character is one element
  12. which makes algorithms a lot easier to implement when you cannot get corrupted
  13. characters or line-breaks by mistake.
  14. ---
  15. Title2: string_load
  16. Loading text from a file using string_load supports UTF-8 and UTF-16.
  17. If no byte order mark is detected, the content is loaded as raw Latin-1
  18. by treating each byte in the file as U+00 to U+FF.
  19. Loading a string from a file using string_load removes carriage-return (U'\r' or 13)
  20. and null terminators (U'\0' or 0).
  21. ---
  22. Title2: string_save
  23. Saving text to a file using string_save lets you select the encodings for
  24. characters and line-breaks.
  25. By default, text is stored as UTF-8 (only takes more space when needed)
  26. with a byte order mark (so that other programs know that it's UTF-8)
  27. and CR-LF line-breaks (so that it can be read on all major desktop systems).
  28. ---
  29. Title2: dsr::String
  30. String is the dynamic text container based on reference counting and immutability.
  31. It guarantees that a head allocated buffer exists when length > 0.
  32. Assigning a String to another will make a shallow copy and increase the buffer's reference count.
  33. Appending more text to or clearing a String sharing its buffers with others will clone the buffer to prevent it from overwriting other strings.
  34. Splitting a String will use reference counting to refer to the same allocation.
  35. Splitting a literal will first create a new heap allocation and then refer to it from all new elements.
  36. ---
  37. Title2: dsr::ReadableString
  38. ReadableString is used instead of String as an input argument so that U"" literals can be given without creating a new allocation.
  39. Accidentally giving a regular "" literal (not UTF-32) will be stopped instead of automatically converted.
  40. If you want to accept giving "" and automatically allocate a buffer for the UTF-32 conversion, then just use String.
  41. See the String as a value and ReadableString as a constant reference.
  42. ---
  43. Title2: dsr::Printable
  44. Inheriting from Printable and defining toStreamIndented allow printing your type using printText (prints to standard output), debugText (only prints in debug mode) and throwError (calls std::runtime_error).
  45. For non-virtual types, you can define string_toStreamIndented with an overload to keep the type simple.
  46. Each of these printing methods allow passing multiple arguments separated by commas.
  47. To print to a new String, give a number of arguments to string_combine.
  48. If you want to keep the existing content and add more text at the end, use string_append.
  49. If appending a character, you probably don't want to print its numerical value, so call string_appendChar for each character being added.
  50. Unlike the << operation, toStreamIndented can take an indentation argument which makes it faster and easier to serialize types into files.
  51. Just let each line begin with the given indentation and then add your own, which can be given to the child components' indentation arguments recursively.
  52. ---