Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Fast way to add null after each char

Reply
Thread Tools

Fast way to add null after each char

 
 
Brad
Guest
Posts: n/a
 
      09-05-2010
std::string s = "easy";

std::string unicode_string;

std::string::const_iterator it,

for(it = s.begin(); it != s.end(); ++it)
{
unicode_string.push_back(*it);
unicode_string.push_back('\0');
}

The above for loop would make unicode_string look like this:

"e null a null s null y null"

Is there a faster way to do this... in place maybe?

Thanks for any tips,

Brad


 
Reply With Quote
 
 
 
 
Francesco S. Carta
Guest
Posts: n/a
 
      09-05-2010
Brad <(E-Mail Removed)>, on 05/09/2010 09:54:28, wrote:

> std::string s = "easy";
>
> std::string unicode_string;
>
> std::string::const_iterator it,
>
> for(it = s.begin(); it != s.end(); ++it)
> {
> unicode_string.push_back(*it);
> unicode_string.push_back('\0');
> }
>
> The above for loop would make unicode_string look like this:
>
> "e null a null s null y null"


Nay, it will make it look like "e\0a\0s\0y\0"... by the way, why do you
need to do such a thing?

> Is there a faster way to do this... in place maybe?


Faster, I don't know (measure it), in place, yes: use the
std::string::insert() method.

--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com
 
Reply With Quote
 
 
 
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      09-05-2010
* Brad, on 05.09.2010 18:54:
> std::string s = "easy";
>
> std::string unicode_string;
>
> std::string::const_iterator it,
>
> for(it = s.begin(); it != s.end(); ++it)
> {
> unicode_string.push_back(*it);
> unicode_string.push_back('\0');
> }
>
> The above for loop would make unicode_string look like this:
>
> "e null a null s null y null"
>
> Is there a faster way to do this... in place maybe?


Depends what you want.

It /seems/ that you're assuming a little-endian architecture, and that the
intent is to treat unicode_string as UTF-16 encoded (via some low level cast),
and that you're assuming that the original character encoding is Latin-1 or a
subset.

That's an awful lot of assumptions.

Look in the standard library for mbcstowcs or something like that, in the C
library, or 'widen'-functions in the C++ library.

Under what seems to be your assumption of Latin-1 encoding of the 'char' string,
and an additional assumption of 16-bit 'wchar_t', you can however do


<code>
#include <iostream>
#include <string>
#include <limits.h>
using namespace std;

#define STATIC_ASSERT( x ) typedef char shouldBeTrue[(x)? 1 : -1]

STATIC_ASSERT( CHAR_BIT == 8 );
STATIC_ASSERT( sizeof( wchar_t ) == 2 );

int main()
{
string const s = "Hello";
wstring const u( s.begin(), s.end() );

wcout << u << L"\n";
}
</code>


But I don't recommend that; use the widening functions, C or C++.


Cheers & hth.,

- Alf

--
blog at <url: http://alfps.wordpress.com>
 
Reply With Quote
 
SG
Guest
Posts: n/a
 
      09-05-2010
On 5 Sep., 19:05, "Francesco S. Carta" wrote:
> in place, yes: use the
> std::string::insert() method.


Or better yet, resize() to final size, assign the non-null characters
in a backwards loop and set a couple of chars to zero:

void sillify(string & io)
{
size_t len1 = io.size();
io.resize(len1*2,'\0');
for (size_t k=len1; k-->1
io[k*2] = io[k];
for (size_t k=1; k<len1; k+=2)
io[k] = '\0';
}

Cheers!
SG
 
Reply With Quote
 
Francesco S. Carta
Guest
Posts: n/a
 
      09-05-2010
SG <(E-Mail Removed)>, on 05/09/2010 10:17:37, wrote:

> On 5 Sep., 19:05, "Francesco S. Carta" wrote:
>> in place, yes: use the
>> std::string::insert() method.

>
> Or better yet, resize() to final size, assign the non-null characters
> in a backwards loop and set a couple of chars to zero:
>
> void sillify(string& io)
> {
> size_t len1 = io.size();
> io.resize(len1*2,'\0');
> for (size_t k=len1; k-->1
> io[k*2] = io[k];
> for (size_t k=1; k<len1; k+=2)
> io[k] = '\0';
> }


Define "better".

void smartify(string& s) {
for(int i = 1, e = s.size()*2; i < e; i+=2) {
s.insert(i, 1, '\0');
}
}

--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com
 
Reply With Quote
 
Marc
Guest
Posts: n/a
 
      09-05-2010
On 5 sep, 19:27, "Francesco S. Carta" <(E-Mail Removed)> wrote:
> SG <(E-Mail Removed)>, on 05/09/2010 10:17:37, wrote:
> > On 5 Sep., 19:05, "Francesco S. Carta" wrote:
> >> in place, yes: use the
> >> std::string::insert() method.

>
> > Or better yet, resize() to final size, assign the non-null characters
> > in a backwards loop and set a couple of chars to zero:

>
> > * * void sillify(string& *io)
> > * * {
> > * * * *size_t len1 = io.size();
> > * * * *io.resize(len1*2,'\0');
> > * * * *for (size_t k=len1; k-->1
> > * * * * * io[k*2] = io[k];
> > * * * *for (size_t k=1; k<len1; k+=2)
> > * * * * * io[k] = '\0';
> > * * }

>
> Define "better".
>
> * * *void smartify(string& s) {
> * * * * *for(int i = 1, e = s.size()*2; i < e; i+=2) {
> * * * * * * *s.insert(i, 1, '\0');
> * * * * *}
> * * *}


Faster. SG's code has linear complexity and yours is quadratic.
Readability is something else...
 
Reply With Quote
 
Francesco S. Carta
Guest
Posts: n/a
 
      09-05-2010
Marc <(E-Mail Removed)>, on 05/09/2010 10:54:46, wrote:

> On 5 sep, 19:27, "Francesco S. Carta"<(E-Mail Removed)> wrote:
>> SG<(E-Mail Removed)>, on 05/09/2010 10:17:37, wrote:
>>> On 5 Sep., 19:05, "Francesco S. Carta" wrote:
>>>> in place, yes: use the
>>>> std::string::insert() method.

>>
>>> Or better yet, resize() to final size, assign the non-null characters
>>> in a backwards loop and set a couple of chars to zero:

>>
>>> void sillify(string& io)
>>> {
>>> size_t len1 = io.size();
>>> io.resize(len1*2,'\0');
>>> for (size_t k=len1; k-->1
>>> io[k*2] = io[k];
>>> for (size_t k=1; k<len1; k+=2)
>>> io[k] = '\0';
>>> }

>>
>> Define "better".
>>
>> void smartify(string& s) {
>> for(int i = 1, e = s.size()*2; i< e; i+=2) {
>> s.insert(i, 1, '\0');
>> }
>> }

>
> Faster. SG's code has linear complexity and yours is quadratic.
> Readability is something else...


Exactly. So neither is better than the other unless we associate
"better" to "more readable" or to "faster"

--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com
 
Reply With Quote
 
Francesco S. Carta
Guest
Posts: n/a
 
      09-05-2010
Francesco S. Carta <(E-Mail Removed)>, on 05/09/2010 20:04:18, wrote:

> Marc <(E-Mail Removed)>, on 05/09/2010 10:54:46, wrote:
>
>> On 5 sep, 19:27, "Francesco S. Carta"<(E-Mail Removed)> wrote:
>>> SG<(E-Mail Removed)>, on 05/09/2010 10:17:37, wrote:
>>>> On 5 Sep., 19:05, "Francesco S. Carta" wrote:
>>>>> in place, yes: use the
>>>>> std::string::insert() method.
>>>
>>>> Or better yet, resize() to final size, assign the non-null characters
>>>> in a backwards loop and set a couple of chars to zero:
>>>
>>>> void sillify(string& io)
>>>> {
>>>> size_t len1 = io.size();
>>>> io.resize(len1*2,'\0');
>>>> for (size_t k=len1; k-->1
>>>> io[k*2] = io[k];
>>>> for (size_t k=1; k<len1; k+=2)
>>>> io[k] = '\0';
>>>> }
>>>
>>> Define "better".
>>>
>>> void smartify(string& s) {
>>> for(int i = 1, e = s.size()*2; i< e; i+=2) {
>>> s.insert(i, 1, '\0');
>>> }
>>> }

>>
>> Faster. SG's code has linear complexity and yours is quadratic.
>> Readability is something else...

>
> Exactly. So neither is better than the other unless we associate
> "better" to "more readable" or to "faster"
>


Just for the records, a better solution, in my opinion, is to build an
appropriately sized new string and copying the original chars at the
appropriate positions - a compromise between readability and speed,
somewhat:

void foo(string& s) {
string r(s.size()*2, '\0');
for(int i = 0, e = s.size(); i < e; ++i) {
r[i*2] = s[i];
}
s.swap(r);
}

ASSUMING that the OP really wants exactly this - WRT Alf P. Steinbach's
notes in the other post.

--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com
 
Reply With Quote
 
SG
Guest
Posts: n/a
 
      09-05-2010
On 5 Sep., 20:04, Francesco S. Carta wrote:
> Marc wrote:
> > On 5 sep, 19:27, Francesco S. Carta wrote:
> >> Define "better".

> > Faster. [...]

> Exactly. So neither is better than the other unless we associate
> "better" to "more readable" or to "faster"


See the original post:

"...Is there a faster way to do this..."

Cheers!
SG
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      09-05-2010
Alf P. Steinbach /Usenet <(E-Mail Removed)> wrote:
> * Brad, on 05.09.2010 18:54:
>> std::string s = "easy";
>>
>> std::string unicode_string;
>>
>> std::string::const_iterator it,
>>
>> for(it = s.begin(); it != s.end(); ++it)
>> {
>> unicode_string.push_back(*it);
>> unicode_string.push_back('\0');
>> }
>>
>> The above for loop would make unicode_string look like this:
>>
>> "e null a null s null y null"
>>
>> Is there a faster way to do this... in place maybe?

>
> Depends what you want.
>
> It /seems/ that you're assuming a little-endian architecture


No, he isn't. He is making the string UTF16LE, not assuming that the
architecture is little-endian.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments