Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Krylloan

Macro to obtain length of a string?

This topic is 5236 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there any way I can obtain the length of a const string AT COMPILE TIME? eg: int a = LENGTH("blah"); becomes int a = 4. thanks

Share this post


Link to post
Share on other sites
Advertisement
Can I ask what''s wrong with strlen()? Sure it''s executed at runtime, but I seriously doubt it''s going to make a difference in speed.

Share this post


Link to post
Share on other sites
It might be a pain in the ass, but if it''s always a const string, you could just count the characters...

Unless you need this for some really funky meta-programming template trick. In which case, I can''t help you.

-Auron

Share this post


Link to post
Share on other sites
If you only plan on using the macro with string literals, this should work:

#define LENGTH(x) (sizeof((x)) - 1)

Share this post


Link to post
Share on other sites
I''m using a string class that has a pointer to the characters and a integer storing the number of characters. I want to write a macro to convert a quotes-enclosed string into the string structure.

The only real reason I don''t want to use strlen is tidiness, because API code (what I''m making is an API) should be the very best possible. (I''m currently using strlen).

There is also the unlikely but not inconsiderable possibility that someone could use a string so often that the repeated use of strlen actually makes a performance difference.

I am currently using the converter to convert the names of all axes from input streams, and there are too many for manually counting them to be tidy / efficient.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Could u do something with maybe bitshifting? Ie each letter is a 8bits so shift down by 8 until zero?

it would probably be easier to make a macro that took several parameters and used that to define your strings...

It''d probably be a recursive preprocessor and thus making it very messy.

May i ask why?

Share this post


Link to post
Share on other sites
quote:
Original post by Auron
It might be a pain in the ass, but if it''s always a const string, you could just count the characters...


You wouldn''t be able to use the preprocessor to count characters if it''s a dynamic thing. Macros being at runtime expanded, though, you could just do...

#define STRING(x) strlen(x)

Or something to that end...

Share this post


Link to post
Share on other sites
Thanks SiCrane, if it works it looks exactly like what I''m after. Here''s hoping sizeof doesn''t word-align.

Share this post


Link to post
Share on other sites
quote:
Original post by SiCrane
If you only plan on using the macro with string literals, this should work:

#define LENGTH(x) (sizeof((x)) - 1)


A string literal resolves to a char*, so...wouldn''t that always be equal to 3? Maybe I''m missing some subtlety in the sizeof operator, I don''t know.

Share this post


Link to post
Share on other sites
SiCrane''s macro is what you want.

But really, use strlen(). Sooner or later, somebody will try to use your macro with a char*, and then you''re stuffed.

A good compiler can provide internal versions of many functions, including strlen(), and will optimise it away for strings whose lengths are known at runtime.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!