Utility Methods - Do These Exist?

May 26, 2014 at 11:59 AM
Edited May 26, 2014 at 12:04 PM
I now have my source code compiling on both Windows and Linux. I have some questions and only need brief answers.

I coded the following utility methods. They work fine but I'm wondering if public methods already exist in the SDK or elsewhere WITH CROSS-PLATFORM SUPPORT?
  1. stricmp(const char *s1, const char *s2) // no, stricmp isn't a standard function
  2. strnicmp(const char *s1, const char *s2, size_t n)
  3. stricmp(const utility::char_t *s1, const utility::char_t *s2)
  4. strnicmp(const utility::char_t *s1, const utility::char_t *s2, size_t n)
  5. stricmp(const utility::string_t &s1, const utility::string_t &s2)
  6. strnicmp(const utility::string &s1, const utility::string_t &s2, size_t n)
  7. utility::char_t *wcsncpy(utility::char_t *dest, size_t destsize, const utility::char_t *src, size_t srcsize)
Bonus questions:
  1. Should I use utility::conversions::to_utf8string().c_str() to convert char_t/string_t to char *, for use in printf("%s",utility::conversions::to_utf8string(mystr).c_str());
  2. When should utility::conversions::print_string() be used?
Bonus Bonus question:
  1. What's the difference between utility::conversions usascii, latin1, utf8. utf16? When to use/not use? Isn't utf16 for Windows only?
May 27, 2014 at 6:01 PM
Hi BSalita,

Take a look at the method bool str_icmp(const utility::string_t &left, const utility::string_t &right) in asyncrt_utils.h. To see one way this could be done. Please note however that this function is under the details namespace and could change between versions at anytime. If you want to propose and make changes consider signing our contributor license agreement and contributing back.

Bonus answer
1. Yes you could. You also could create a prinf_t which would use printf/wprintf correctly depending on the platform. This would avoid conversions in all cases.
2. print_string can be used to convert any type which supports the '<<' operator to a string. If you take a look at the implementation it is straight forward.
Bonus bonus answer
1. Yes in general we are using utf16 everywhere for Windows. However there are situations where we could end up with a string in latin1 that came across the network and we need to convert it to Unicode. If you look at for example the implementation of http_response::extract_string() you can see this being used.