Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the (printed) length of a std::string with tab characters expanded?

Tags:

c++

string

Given a C++ std::string variable which includes tab characters, is it possible to determine the length of that string as it would appear on the "screen"? i.e.:

std::string var = "\t\t\t";
std::cout << var.length();          // result: 3
std::cout << printed_length(var);   // result: 3*(# of spaces per tab)
like image 237
e.James Avatar asked Nov 29 '11 17:11

e.James


People also ask

How do I find the length of a std::string?

When dealing with C++ strings (std::string), you're looking for length() or size(). Both should provide you with the same value. However when dealing with C-Style strings, you would use strlen(). Save this answer.

What is the length of the tab?

The default value for the tab-size property is 8 space characters, and it can accept any positive integer value.

How do you assign the length of a string?

The Java String length() method is a method that is applicable for string objects. length() method returns the number of characters present in the string. The length() method is suitable for string objects but not for arrays. The length() method can also be used for StringBuilder and StringBuffer classes.

What is a Wstring C++?

This function is used to convert the numerical value to the wide string i.e. it parses a numerical value of datatypes (int, long long, float, double ) to a wide string. It returns a wide string of data type wstring representing the numerical value passed in the function.


1 Answers

Not easily. It's impossible without specific knowledge of the "screen" involved (really, the software driving the output), because tab expansion varies so widely. There are four fairly obvious possibilities, based on fixed expansion vs. expansion to a multiple of something, and based on character cells vs. some other fixed measurement (e.g., for proportional fonts). There are also "smart tabs" with even more complex criteria, where one tab's expansion may depend upon another tab.

On a typical "console" that'll be expansion mod 8 character cells. To deal with that, you'll not only have to count the tabs, but also look at the position of each tab in the string. You'll also have to make some assumptions (or provide a parameter) about the position of the beginning of the string relative to a tab stop.

Bottom line: if you want to do something like this, you'll have to do it yourself, based on knowledge of how tabs will be expanded on your target.

like image 109
Jerry Coffin Avatar answered Nov 15 '22 01:11

Jerry Coffin