// -*- Pike -*- // // trim.pmod // // The rtrim, ltrim and trim methods mimics the equivalents in PHP, // www.php.net, except they probably don't work on binary strings and the // reason for that is that I simply don't understand them ;) // // This is no rocket science so no license apply. Do what you want... // // @author Pontus Östlund // Modified and improved by Jonas Walldén at Roxen IS (www.roxen.com) // // Tab size 2 // Indent width 2 #pike __REAL_VERSION__ //! Trim trailing whitespace or charcter or charcaters @[char] //! from @[in]. Whitout the @[char] paramter these characters will be stripped //! //! @string //! @value " " //! a blank space //! @value "\t" //! a tab //! @value "\n" //! a new line (line feed) //! @value "\r" //! a carriage return //! @value "\0" //! the @i{NUL@}-byte" //! @endstring string rtrim(string in, string|void char) { return reverse(ltrim(reverse(in), char)); } //! Trim leading whitespace or charcter or charcaters @[char] //! from @[in]. Whitout the @[char] paramter these characters will be stripped //! //! @string //! @value " " //! a blank space //! @value "\t" //! a tab //! @value "\n" //! a new line (line feed) //! @value "\r" //! a carriage return //! @value "\0" //! the @i{NUL@}-byte" //! @endstring string ltrim(string in, string|void char) { if (char && sizeof(char)) { if (has_value(char, "-")) char = (char - "-") + "-"; if (has_value(char, "]")) char = "]" + (char - "]"); if (char == "^") { // Special case for ^ since that can't be represented in the sscanf // set. We'll expand the set with a wide character that is illegal // Unicode and hence won't be found in regular strings. char = "\xFFFFFFFF^"; } sscanf(in, "%*[" + char + "]%s", in); } else sscanf(in, "%*[ \n\r\t\0]%s", in); return in; } //! Trim leading and trailing whitespcace or charcter or charcaters @[char] //! from @[in]. Whitout the @[char] paramter these characters will be stripped //! //! @string //! @value " " //! a blank space //! @value "\t" //! a tab //! @value "\n" //! a new line (line feed) //! @value "\r" //! a carriage return //! @value "\0" //! the @i{NUL@}-byte" //! @endstring string trim(string in, string|void char) { return (char && sizeof(char)) ? rtrim(ltrim(in, char), char) : String.trim_all_whites(in); } //! Shortens @[in] to @[len] characters by trimming from the center and out. //! The left and right part will be concatenated with @[glue] or //! @expr{"..."@} as default. @[cut_betwwen_words] defines wether or not to to //! cut between words, that is at a blank space. //! //! If @[in] is shorter or equal to @[len] the string will be returned //! untouched. //! //! @note //! The resulting string may be shorter than @[len] due to whitespace trimming //! but will never exceed the maximum length. //! //! @example //! string my_str = "A pretty long and meaningless string without purpose"; //! write("%s", ctrim(my_str, 30), 1); //! Result: "A pretty long...without purpose" string ctrim(string in, int len, int cut_between_words, string|void glue) { if (sizeof(in) <= len) return in; // Take glue string into account when computing max length glue = glue || "..."; if (sizeof(glue) > len) RXML.run_error("Glue string longer than requested length.\n"); len -= sizeof(glue); int right_len = len / 2; int left_len = len - right_len; string left = String.trim_all_whites(in[..left_len - 1]); string right = String.trim_all_whites(in[sizeof(in) - right_len..]); // Cut between words if needed if (cut_between_words) { // Only search for word delimiters if we haven't already cut at one int pos; if (in[sizeof(left)] != ' ') if ((pos = search(reverse(left), " ")) > -1) left = left[..sizeof(left) - pos - 2]; if (in[sizeof(in) - sizeof(right) - 1] != ' ') if ((pos = search(right, " ")) > -1) right = right[pos + 1..]; } return left + glue + right; }