By Daniel · January 7, 2009
Trims characters from the end of a string until it is no wider than max_width. If trimming is necessary, the suffix (typically "...") will be appended on to the end of the string. Runs in logarithmic time.
/* string_confine_width(str, max_width, suffix)
*
* Trims characters from the end of a string until it is no wider than
* max_width. If trimming is necessary, the suffix (typically "...") will
* be appended on to the end of the string. Runs in logarithmic time.
*/
if (string_width(argument0) <= argument1)
return argument0;
var lo, hi, mid;
lo = 0;
hi = string_length(argument0);
while (hi - lo > 1) {
mid = (lo + hi) div 2;
if (string_width(string_copy(argument0, 1, mid) + argument2) <= argument1)
lo = mid;
else
hi = mid;
}
return string_copy(argument0, 1, lo) + argument2;
Categories: String handling
There are no comments to display.
You must be signed in to post comments.