Browse Source

add char* strdup_NFC(string_view s). FIXME: The more efficient implementation needs some more internal re-work, that's why this correct but stupid quick'n'dirty implementation is there for now.

master
roker 8 months ago
parent
commit
c48088d788
2 changed files with 39 additions and 0 deletions
  1. +35
    -0
      src/nfc.cc
  2. +4
    -0
      src/nfc.hh

+ 35
- 0
src/nfc.cc View File

@ -11,6 +11,8 @@
#include "nfc_sets.hh"
#include <pEp/pEp_string.h>
namespace
{
// unicode to hex string
@ -628,6 +630,39 @@ std::basic_string<CharT> toNFC(basic_string_view<CharT> s)
}
// convenience function to avoid ::strdup(pEp::toNFC<char>(text).c_str());
// and unecessary temporary std::string etc.
char* strdup_NFC(string_view s)
{
if(isNFC_quick_check(s)==IsNFC::Yes)
return ::new_string(s.data(), s.size());
// implement the hard way more efficient
/********** FIXME: need more re-work, so I'll do the dumb way first
const std::u32string& u32 = createNFC( fromUtf_decompose(s) );
const size_t out_len = utf8len(u32);
char* ret = ::new_string(nullptr, out_len );
char* iter{ret};
for(const char32_t c : u32)
{
toUtf<char, char*>(c, iter);
}
if(iter > ret+out_len) // should never happen. ;)
{
throw std::logic_error("internal error: strdup_NFC() exceeded output string size");
}
return ret;
********************/
// Correct but inefficient:
const std::string ret = toNFC<char>(s);
return ::new_string(ret.data(), 0);
}
// used only to initialize the NFC Compose mapping:
std::map< std::pair<unsigned, unsigned>, unsigned> generate_nfc_compose()
{


+ 4
- 0
src/nfc.hh View File

@ -68,6 +68,10 @@ std::basic_string<CharT> toNFC(basic_string_view<CharT> s);
// creates a UTF-8-encoded NFC string from s
std::string toNFC_8(u16string_view s);
// convenience function to avoid ::strdup(pEp::toNFC<char>(text).c_str());
// and unecessary temporary std::string etc.
char* strdup_NFC(string_view s);
} // end of namespace pEp


Loading…
Cancel
Save