C++ formatting into a buffer - why so elephanting difficult?
-
The good news is that this isn't a homework question or comment - I'm tossing this out to C++ veterans for some direction. I'd ask in S.O. but ugh, I'm sure I'd get shamed or start a flame war on why this is a redundant question. I cut my development teeth on C and happily moved on to C++. I have also worked in a mixed development shop for what seems like forever. Most of the UI development is in C++ of some flavor - some variant of MS depending on what tool we need to use, other tools from other vendors. However, we're always talking to embedded systems, and most of the guys are almost always pure C developers - even if the do run it through a C++ compiler :). For the C++ code, I would love to be pure C++, especially when it comes to string handling. Current development for one side is in VS2008, and we have a mix of C++ std::wstring and CStrings (mfc). On the embedded side, usually std::wstring but mostly char bufs. I really don't care that much about the embedded side (actually I do), but I would surely love to come up with a general approach on my side. Which leads me to the question - why is it so elephanting difficult to format a string into a buffer in standard C++? I know I can add on other libraries, but for the sake of discussion, let's say I'm not allowed to use boost, mfc, etc. Sure, I've done my google, but it seems to me that C++ has taken purity to the level of uselessness. I'm sure I'll be burned at the stake for that, or maybe I've not found my "aha!" moment.
Charlie Gilley <italic>Stuck in a dysfunctional matrix from which I must escape... "Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
-
The good news is that this isn't a homework question or comment - I'm tossing this out to C++ veterans for some direction. I'd ask in S.O. but ugh, I'm sure I'd get shamed or start a flame war on why this is a redundant question. I cut my development teeth on C and happily moved on to C++. I have also worked in a mixed development shop for what seems like forever. Most of the UI development is in C++ of some flavor - some variant of MS depending on what tool we need to use, other tools from other vendors. However, we're always talking to embedded systems, and most of the guys are almost always pure C developers - even if the do run it through a C++ compiler :). For the C++ code, I would love to be pure C++, especially when it comes to string handling. Current development for one side is in VS2008, and we have a mix of C++ std::wstring and CStrings (mfc). On the embedded side, usually std::wstring but mostly char bufs. I really don't care that much about the embedded side (actually I do), but I would surely love to come up with a general approach on my side. Which leads me to the question - why is it so elephanting difficult to format a string into a buffer in standard C++? I know I can add on other libraries, but for the sake of discussion, let's say I'm not allowed to use boost, mfc, etc. Sure, I've done my google, but it seems to me that C++ has taken purity to the level of uselessness. I'm sure I'll be burned at the stake for that, or maybe I've not found my "aha!" moment.
Charlie Gilley <italic>Stuck in a dysfunctional matrix from which I must escape... "Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
Quote:
For the C++ code, I would love to be pure C++, especially when it comes to string handling.
Why would you want to avoid C library functions? If you have a look at some STL implementations for string types, you will notice that those use C library functions like
memcpy
,memmove
,memset
, andmemchr
. But you are right. I don't like the C++ string formatting too. Therefore, I never use them (besides sometimes with console output). When using other libraries like MFC, Boost, or Qt is not an option I still use[s|f][n][w]printf
or the corresponding*_s
versions with VS. -
The good news is that this isn't a homework question or comment - I'm tossing this out to C++ veterans for some direction. I'd ask in S.O. but ugh, I'm sure I'd get shamed or start a flame war on why this is a redundant question. I cut my development teeth on C and happily moved on to C++. I have also worked in a mixed development shop for what seems like forever. Most of the UI development is in C++ of some flavor - some variant of MS depending on what tool we need to use, other tools from other vendors. However, we're always talking to embedded systems, and most of the guys are almost always pure C developers - even if the do run it through a C++ compiler :). For the C++ code, I would love to be pure C++, especially when it comes to string handling. Current development for one side is in VS2008, and we have a mix of C++ std::wstring and CStrings (mfc). On the embedded side, usually std::wstring but mostly char bufs. I really don't care that much about the embedded side (actually I do), but I would surely love to come up with a general approach on my side. Which leads me to the question - why is it so elephanting difficult to format a string into a buffer in standard C++? I know I can add on other libraries, but for the sake of discussion, let's say I'm not allowed to use boost, mfc, etc. Sure, I've done my google, but it seems to me that C++ has taken purity to the level of uselessness. I'm sure I'll be burned at the stake for that, or maybe I've not found my "aha!" moment.
Charlie Gilley <italic>Stuck in a dysfunctional matrix from which I must escape... "Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
-
Quote:
For the C++ code, I would love to be pure C++, especially when it comes to string handling.
Why would you want to avoid C library functions? If you have a look at some STL implementations for string types, you will notice that those use C library functions like
memcpy
,memmove
,memset
, andmemchr
. But you are right. I don't like the C++ string formatting too. Therefore, I never use them (besides sometimes with console output). When using other libraries like MFC, Boost, or Qt is not an option I still use[s|f][n][w]printf
or the corresponding*_s
versions with VS.No particular reason, and your point is valid that the lower level methods use these functions. I guess it's the purist in me. I have a (well it was in the past) multi-platform base of software that has a hodge-podge of string formatting code. I've got CStrings going to std::wstrings and back, which just seems silly to me. So, I said to myself, let's just use C++ and move away from mfc. Silly me.
Charlie Gilley <italic>Stuck in a dysfunctional matrix from which I must escape... "Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
-
I have recently been using
stringstream
s (<sstream> | Microsoft Docs[^]), and I have to say they are actually easier than at first sight.I'll take a look. It's funny, people have been raging about string formatting in C++ for years/decades. The most common answer is "use boost", which I feel is an indictment of C++ :)
Charlie Gilley <italic>Stuck in a dysfunctional matrix from which I must escape... "Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
-
The good news is that this isn't a homework question or comment - I'm tossing this out to C++ veterans for some direction. I'd ask in S.O. but ugh, I'm sure I'd get shamed or start a flame war on why this is a redundant question. I cut my development teeth on C and happily moved on to C++. I have also worked in a mixed development shop for what seems like forever. Most of the UI development is in C++ of some flavor - some variant of MS depending on what tool we need to use, other tools from other vendors. However, we're always talking to embedded systems, and most of the guys are almost always pure C developers - even if the do run it through a C++ compiler :). For the C++ code, I would love to be pure C++, especially when it comes to string handling. Current development for one side is in VS2008, and we have a mix of C++ std::wstring and CStrings (mfc). On the embedded side, usually std::wstring but mostly char bufs. I really don't care that much about the embedded side (actually I do), but I would surely love to come up with a general approach on my side. Which leads me to the question - why is it so elephanting difficult to format a string into a buffer in standard C++? I know I can add on other libraries, but for the sake of discussion, let's say I'm not allowed to use boost, mfc, etc. Sure, I've done my google, but it seems to me that C++ has taken purity to the level of uselessness. I'm sure I'll be burned at the stake for that, or maybe I've not found my "aha!" moment.
Charlie Gilley <italic>Stuck in a dysfunctional matrix from which I must escape... "Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
I hear you!! String formatting is a PITA in C++. a "fun" youtube video about string formatting. [CppCon 2017: Victor Zverovich “A modern formatting library for C++” - YouTube](https://www.youtube.com/watch?v=ptba\_AqFYCM)
I'd rather be phishing!
-
I hear you!! String formatting is a PITA in C++. a "fun" youtube video about string formatting. [CppCon 2017: Victor Zverovich “A modern formatting library for C++” - YouTube](https://www.youtube.com/watch?v=ptba\_AqFYCM)
I'd rather be phishing!
Max - lol, I thought this was going to be some stand up comedy regarding formatting! I'll be sure to watch it. Good post.
Charlie Gilley <italic>Stuck in a dysfunctional matrix from which I must escape... "Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
-
The good news is that this isn't a homework question or comment - I'm tossing this out to C++ veterans for some direction. I'd ask in S.O. but ugh, I'm sure I'd get shamed or start a flame war on why this is a redundant question. I cut my development teeth on C and happily moved on to C++. I have also worked in a mixed development shop for what seems like forever. Most of the UI development is in C++ of some flavor - some variant of MS depending on what tool we need to use, other tools from other vendors. However, we're always talking to embedded systems, and most of the guys are almost always pure C developers - even if the do run it through a C++ compiler :). For the C++ code, I would love to be pure C++, especially when it comes to string handling. Current development for one side is in VS2008, and we have a mix of C++ std::wstring and CStrings (mfc). On the embedded side, usually std::wstring but mostly char bufs. I really don't care that much about the embedded side (actually I do), but I would surely love to come up with a general approach on my side. Which leads me to the question - why is it so elephanting difficult to format a string into a buffer in standard C++? I know I can add on other libraries, but for the sake of discussion, let's say I'm not allowed to use boost, mfc, etc. Sure, I've done my google, but it seems to me that C++ has taken purity to the level of uselessness. I'm sure I'll be burned at the stake for that, or maybe I've not found my "aha!" moment.
Charlie Gilley <italic>Stuck in a dysfunctional matrix from which I must escape... "Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
charlieg wrote:
why is it so elephanting difficult to format a string into a buffer in standard C++?
What does that mean exactly? Both 'format' and in general why would you think that something would make it easy. As I recall as to what I think you are referring in the cases in the past if I had a specific fixed need to put/get values into a buffer then I just wrote a class that did it appropriately. It would have get/set methods that would do the correct thing for the buffer. Then there were a couple of management methods that set the buffer up, extracted it, etc. Similar but simpler than the way XML/Json libraries work. And long ago I created at least one implementation that did things similar too those with multiple types and I did it in C++.
-
I'll take a look. It's funny, people have been raging about string formatting in C++ for years/decades. The most common answer is "use boost", which I feel is an indictment of C++ :)
Charlie Gilley <italic>Stuck in a dysfunctional matrix from which I must escape... "Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
charlieg wrote:
"use boost", which I feel is an indictment of C++
I rather think it is, "Oh, C++ string handling looks difficult, I'll find a different library". Yes, there is a learning curve but that is true of just about everything. You can probably guess that I have never used boost. ;)
-
charlieg wrote:
why is it so elephanting difficult to format a string into a buffer in standard C++?
What does that mean exactly? Both 'format' and in general why would you think that something would make it easy. As I recall as to what I think you are referring in the cases in the past if I had a specific fixed need to put/get values into a buffer then I just wrote a class that did it appropriately. It would have get/set methods that would do the correct thing for the buffer. Then there were a couple of management methods that set the buffer up, extracted it, etc. Similar but simpler than the way XML/Json libraries work. And long ago I created at least one implementation that did things similar too those with multiple types and I did it in C++.
For example - suppose I want to format a log message. Beloved printf/sprintf: sprintf(&buf[0], "Now is the time (%s) for all good %s blah blah.\r\n", GetTime(), "men"); I could add other items to the log message, say hex values, floating point with fixed # of decimal, etc. Why would you write a class for doing something like this? It's admittedly adhoc. I'm trying to do something like this in C++, and I just don't see it. As I said, most of the hits from google are "use boost::format". I confess I just may have missed the obvious....
Charlie Gilley <italic>Stuck in a dysfunctional matrix from which I must escape... "Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
-
For example - suppose I want to format a log message. Beloved printf/sprintf: sprintf(&buf[0], "Now is the time (%s) for all good %s blah blah.\r\n", GetTime(), "men"); I could add other items to the log message, say hex values, floating point with fixed # of decimal, etc. Why would you write a class for doing something like this? It's admittedly adhoc. I'm trying to do something like this in C++, and I just don't see it. As I said, most of the hits from google are "use boost::format". I confess I just may have missed the obvious....
Charlie Gilley <italic>Stuck in a dysfunctional matrix from which I must escape... "Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
charlieg wrote:
For example - suppose I want to format a log message. Beloved printf/sprintf: sprintf(&buf[0], "Now is the time (%s) for all good %s blah blah.\r\n", GetTime(), "men");
First I wouldn't roll my own log, but that gets back to your point of not using a library. So if no library then I would write my own log api, and it would include a varargs log methods (one for each filter type.) And I would just implement a standard varargs method to do the write. I have done exactly that at least twice before standard log libraries existed.
charlieg wrote:
Why would you write a class for doing something like this?
The examples in the past, to which I thought you might have been referring were in reference to populated a piece of binary data, for message protocols, which must have a very specific format. For example (2 byte positive int, 4 char identifier, 20 char text)
-
For example - suppose I want to format a log message. Beloved printf/sprintf: sprintf(&buf[0], "Now is the time (%s) for all good %s blah blah.\r\n", GetTime(), "men"); I could add other items to the log message, say hex values, floating point with fixed # of decimal, etc. Why would you write a class for doing something like this? It's admittedly adhoc. I'm trying to do something like this in C++, and I just don't see it. As I said, most of the hits from google are "use boost::format". I confess I just may have missed the obvious....
Charlie Gilley <italic>Stuck in a dysfunctional matrix from which I must escape... "Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
-
The good news is that this isn't a homework question or comment - I'm tossing this out to C++ veterans for some direction. I'd ask in S.O. but ugh, I'm sure I'd get shamed or start a flame war on why this is a redundant question. I cut my development teeth on C and happily moved on to C++. I have also worked in a mixed development shop for what seems like forever. Most of the UI development is in C++ of some flavor - some variant of MS depending on what tool we need to use, other tools from other vendors. However, we're always talking to embedded systems, and most of the guys are almost always pure C developers - even if the do run it through a C++ compiler :). For the C++ code, I would love to be pure C++, especially when it comes to string handling. Current development for one side is in VS2008, and we have a mix of C++ std::wstring and CStrings (mfc). On the embedded side, usually std::wstring but mostly char bufs. I really don't care that much about the embedded side (actually I do), but I would surely love to come up with a general approach on my side. Which leads me to the question - why is it so elephanting difficult to format a string into a buffer in standard C++? I know I can add on other libraries, but for the sake of discussion, let's say I'm not allowed to use boost, mfc, etc. Sure, I've done my google, but it seems to me that C++ has taken purity to the level of uselessness. I'm sure I'll be burned at the stake for that, or maybe I've not found my "aha!" moment.
Charlie Gilley <italic>Stuck in a dysfunctional matrix from which I must escape... "Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
-
Quote:
sprintf(&buf[0], "Now is the time (%s) for all good %s blah blah.\r\n", GetTime(), "men");
That would be
ostringstream oss;
oss << "Now is the time (" << GetTime() << ") for all good " << "men" << " blah blah." << endl;
string buf = oss.str();Following up on this, I'll have to dig into that. That approach looks simple enough. Of course, the GetTime was a make believe function. Funny, I would think that I would have seen something like this under "Formatting Text in C++" or what have you. I'll dig for more examples. Thanks:thumbsup:
Charlie Gilley <italic>Stuck in a dysfunctional matrix from which I must escape... "Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
-
Following up on this, I'll have to dig into that. That approach looks simple enough. Of course, the GetTime was a make believe function. Funny, I would think that I would have seen something like this under "Formatting Text in C++" or what have you. I'll dig for more examples. Thanks:thumbsup:
Charlie Gilley <italic>Stuck in a dysfunctional matrix from which I must escape... "Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759