Advertisement
Guest User

BlockFormatString

a guest
Jul 2nd, 2016
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. /*All credit for everything in this function goes to user "Epic" at the zdoom forums*/
  2. /*some notes on performance: This function manages strings of about 1152 characters if charsperline is set to 10,
  3. and more than 3k character strings at 80+ CharsPerLine. I haven't tested empirically or anything though.*/
  4.  
  5.  
  6. /*
  7. This function is intended to return a String that has been properly
  8. formatted in a block while breaking at appropriate places to prevent
  9. words from being cut off.
  10.  
  11. For example, if you were to do:
  12.  
  13. str text = "The quick brown fox jumps over the lazy dog.";
  14. print(s:blockFormatString(text, 16));
  15.  
  16. What will show up on the screen is:
  17. The quick brown
  18. fox jumps over
  19. the lazy dog.
  20.  
  21. Escape sequences and colors are not handled!!!
  22. (E.g., \n, \cx, \t)
  23.  
  24. For a new line, use the bar | character!
  25. */
  26.  
  27. int lineCount = 0;
  28.  
  29. function str blockFormatString(str inputString, int charsPerLine) {
  30. str Result = "";
  31.  
  32. //reset this to 0 so it can be reused on new calls
  33. lineCount = 0;
  34.  
  35. //Loop through and determine how to divvy out the lines.
  36. while(strlen(inputString) > charsPerLine) {
  37. //Check to see if the string has the escape character: |
  38. //This is wrapped in an if else block due to possibility of breaking if the string to modify is around
  39. //charsPerLine and the leftovers from this function get run through the next if/else block. For example,
  40. //if we had "The Quick Brown fox jumps over the lazy dog. |The quick", at the end of a string, with a
  41. //charsPerLine of say 30, "The quick" would be leftover and would not be able to run through the if or else
  42. //blocks because both use getChar, which would return an error, because there isn't a 30th position in that string.
  43. //So thus we restart the while loop to recheck the condition.
  44.  
  45. //Create a copy of the theoretical line that we can check through and modify.
  46. str tempStr = stringSubstring(inputString, 0, charsPerLine);
  47. if(stringContains(tempStr,"|")) {
  48. //keep track of the last | position so we know what to chop off of inputString.
  49. int lastPosition = stringFindLast(tempStr, "|") + 1; //note index starts at 0
  50. //using this because stringRemove has a glitch where it will not remove the last character.
  51. tempStr = stringSubstring(tempStr, 0, lastPosition);
  52. //Loop through the string
  53. for(int index = 0; index < strlen(tempStr); index++) {
  54. //check the string for '|' and add to the linecount for each one.
  55. if(getChar(tempstr, index) == '|') {
  56. lineCount++;
  57. }
  58. }
  59. tempstr = stringReplace(tempstr, "|", "\n", 0, 0);
  60. //Add this to the result, remove the stuff we just added
  61. result = concatenateStrings(2, result, tempstr, "", "");
  62. inputString = stringRemove(inputString, 0, lastPosition);
  63.  
  64. } else {
  65.  
  66. //check the char after the end of the line to check if it's a space
  67. if(getChar(inputString, charsPerLine) == ' ') {
  68. //If so, concatenate the proper text into the result.
  69. result = concatenateStrings(3, result, stringSubstring(inputString, 0, charsPerLine + 1), "\n", "");
  70. //Clean out the stuff we just concatenated into the result
  71. inputString = StringRemove(inputString, 0, charsPerLine + 1);
  72. lineCount++;
  73. //Else, FIND A SPACE TO BREAK AT!
  74. } else {
  75. bool found = false;
  76. //Cycle backwards through the string until a space is found (or run out of characters).
  77. //Initialze our index here at charsPerLine - 1 because the index in Strings starts at 0.
  78. for(int i = charsPerLine - 1; i > 0 && !found; i--) {
  79. //Check for a space at the current character
  80. if(getChar(inputString, i) == ' ') {
  81. found = true;
  82.  
  83. //the i + 1 is to copy the found space.
  84. result = concatenateStrings(4, result, stringSubstring(inputString, 0, i + 1), "", "\n");
  85. inputString = stringRemove(inputString, 0, i + 1);
  86. lineCount++;
  87. }
  88. }
  89.  
  90. //If no space was found, break the line off anyway.
  91. if(!found) {
  92. result = concatenateStrings(3, result, stringSubstring(inputString, 0, charsPerLine), "\n", "");
  93. inputString = stringRemove(inputString, 0, charsPerLine);
  94. lineCount++;
  95. }
  96. }
  97. }
  98. }
  99.  
  100. //And this takes care of a hanging line.
  101. if(strlen(inputString) > 0) {
  102. //loop through the remainder of the string and check for |
  103. for(int index2 = 0; index2 < strlen(inputString); index2++) {
  104. //check the string for '|' and add to the linecount for each one.
  105. if(getChar(inputString, index2) == '|') {
  106. lineCount++;
  107. }
  108. }
  109. //replace the | with \n
  110. inputString = stringReplace(inputString, "|", "\n", 0, 0);
  111. result = concatenateStrings(2, result, inputString, "", "");
  112. lineCount++;
  113. }
  114.  
  115. return result;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement