var kText/kText = new kText var const // padText() sides PAD_LEFT = 1 PAD_RIGHT = 2 PAD_BOTH = 3 // centers the text // ascii values, for convenience. ASCII_SPACE = 32 ASCII_LINEBREAK = 10 ASCII_TAB = 9 proc /** * Gets the character at the given position in the string. * @param string The string to be queried. * @param pos The position of the character to be gotten. * @return the character at the given position in the given string. */ getCharacter(string, pos=1) return ascii2text(text2ascii(string, pos)) /** * Check if the character at the given position in the string is whitespace. * @param string The string to be queried. * @param pos The position to be checked. * @return TRUE if it is whitespace. FALSE otherwise. */ isWhitespace(string, pos=1) var/ascii = text2ascii(string, pos) if( ascii == ASCII_SPACE ||\ ascii == ASCII_LINEBREAK ||\ ascii == ASCII_TAB) return TRUE return FALSE /** * Capitalize the first character in a string. * @param string The string to be capitalized. * @return the capitalized form of the string. */ capitalize(string) if(length(string)==1) return uppertext(string) return uppertext(ascii2text(text2ascii(string, 1)))+copytext(string, 2) /** * Removes preceeding and following whitespace in a string. * @param string The string to be trimmed. * @return the trimmed string. * IE: trimWhitespace(" this is a string ] ") would return "this is a string ]". */ trimWhitespace(string) var/start=1, end=length(string) for(start; startstart; end--) if(!isWhitespace(string, end)) break return copytext(string, start, end+1) /** * Finds the next occurrence of white space in a string, starting at the given position. * @param string The string to be queried. * @param pos Where to start searching for whitespace. * @return the position of the next occurrence of whitespace, or null of there is no occurrence. */ findNextWhitespace(string, pos=1) for(var/i=pos; i<=length(string); i++) if(isWhitespace(string, i)) return i /** * Finds the xth occurence of a string within another string. * @param string The string to be queried. * @param sub The string to search for. * @param which Which occurrence of the string [sub] to look for. * @return the starting position of the xth (which) occurrence of [sub] within [string]. * 0 if nothing is found. * IE: findWhich("a b a", "a", 2) would return 5 (where the second occurrence of "a" is). * if which is -1, it will find the last occurrence of the substring. */ findWhich(string, sub, which=1) if(which==1) return findtext(string, sub) var/last = findtext(string, sub) if(!last) return 0 // find the LAST occurrence if(which == -1) var/find = findtext(string, sub, last+length(sub)+1) while(find) last = find find = findtext(string, sub, last+length(sub)+1) return last // find whichever occurrence for(var/i=2 to which) var/find = findtext(string, sub, last+length(sub)+1) if(!find) return 0 last = find return last /** * The case-sensitive version of findWhich(). * @see kText.findWhich() */ findWhichCase(string, sub, which=1) if(which==1) return findtextEx(string, sub) var/last = findtextEx(string, sub) if(!last) return 0 for(var/i=2 to which) var/find = findtextEx(string, sub, last+length(sub)+1) if(!find) return 0 last=find return last /** * Converts a string into a list, using a delimiter to determine where to separate entries. * @param string The string to be listified. * @param delimiter The string that will serve as a separator between entries in the list. * @return the list form of the string. * IE: text2list("this is a test", " ") would return list("this", "is", "a", "test"). */ text2list(string, delimiter=" ") var/list/listified = new, last=1 for(var/find=findtext(string, delimiter); find; find=findtext(string, delimiter, find+length(delimiter))) listified += copytext(string, last, find) last=find+length(delimiter) listified += copytext(string, last) return listified /** * The case-sensitive version of text2list(). * @see kText.text2list() */ text2listCase(string, delimiter=" ") var/list/listified = new, last=1 for(var/find=findtextEx(string, delimiter); find; find=findtextEx(string, delimiter, find+length(delimiter))) listified += copytext(string, last, find) last=find+length(delimiter) listified += copytext(string, last) return listified /** * Converts a list into a string, placing a delimiter inbetween entries in the list. * @param list The list to be textified. * @param delimiter The string to place inbetween list entries. * @return the string form of the list. * IE: list2text(list("this", "is", "a", "test"), " ") would return "this is a test". */ list2text(list/list, delimiter=" ") if(!list || !islist(list) || list.len < 1) return null var/stringified = "[list[1]]" for(var/i=2 to list.len) stringified += "[delimiter][list[i]]" return stringified /** * Replaces all occurrences of one string within another string, with another string. * @param string The string to be queried. * @param sub The string to be searched for. * @param replace The string to be used as a replacement. * @return the given string, with all occurrences of [sub] replaced with [replace]. * IE: replaceText("i love cake", "cake", "pie") would return "i love pie". */ replaceText(string, sub, replace) var/replacified, last=1 for(var/find=findtext(string, sub); find; find=findtext(string, sub, find+length(sub))) replacified += copytext(string, last, find) + replace last=find+length(sub) replacified += copytext(string, last) return replacified /** * The case-sensitive version of replaceText(). * @see kText.replaceText() */ replaceTextCase(string, sub, replace) var/replacified, last=1 for(var/find=findtextEx(string, sub); find; find=findtextEx(string, sub, find+length(sub))) replacified += copytext(string, last, find) + replace last=find+length(sub) replacified += copytext(string, last) return replacified /** * Pads a string so it is the given size. * @param string The string to be padded. * @param size The size the string should be padded to. * @param padSide The side to place the padding on the string. * @param padText The text to be used to pad the string. * @return the padded form of the given string. * IE: padText("Attributes:", 15, PAD_LEFT) would return " Attributes:". * * note: When using PAD_BOTH, the given text will attempt to be centered. * This function assumes a fixed-width font, which means if the size * of the padding is uneven, then one side (the right side) will be * given an extra padding. * * For example, padText("ABC", 6, PAD_BOTH, "-") would require atleast 3 * padding characters. Since this can't be split down the middle, * one of the padding characters will be added to the end of the string: * "-ABC--". */ padText(string, size=12, padSide=PAD_LEFT, padCharacter=" ") var/difference = size - length(string) if(difference < 1) return string switch(padSide) // they both do the same thing, so just switch around the return values. if(PAD_RIGHT, PAD_LEFT) var/pad for(var/i=1 to difference) pad += padCharacter return (padSide == PAD_RIGHT ? "[string][pad]" : "[pad][string]") if(PAD_BOTH) var/pad for(var/i=1 to round(difference/2)) pad += padCharacter return "[pad][string][pad][difference % 2 ? padCharacter : null]" /** * Removes linebreaks from the given string, replacing them with spaces. * @param string The string to be modified. * @return The string, with linebreaks replaced with spaces. */ noBreak(string) return replaceText(string, "\n", " ") /** * Adds linebreaks to the string every X characters. * If the character at the position we want to place the linebreak at isn't whitespace, * it will look for the next whitespace character before placing it. * @param string The string to be modified. * @param charsPerLine How many characters should be on each separate line. * @return The modified string, with linebreaks placed every X characters. * IE: autoBreak("I am not a crook.", 5) would return "I am\nnot a\ncrook." * * note: To keep the newly created lines consistent, this will also remove any * extra whitespace at the beginning and ends of newly added lines. */ autoBreak(string, charsPerLine=50) var/list/breakified = new var/last=1 while(isWhitespace(string, last)) last++ for(var/i=last+charsPerLine; i length) return copytext(string, 1, length)