473,320 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Splitting a string

Hi!

I've got an input wchar_t array in a callback function from expat:

void my_callback(const wchar_t* data);

Additionally, I got a second function that I need to call within my
callback with:

void my_call_to(const wchar_t** arr);

Now the issue I got is that I am in the need to split of the <data>
array in two or max. three tokens, delimited wiht a | char. Then I
need to push it into a local array like

const wchar_t* arr[3] = {0,0,0};

and then put this array over to the my_call_to function so that it can
use it. I want to use chars on the heap so that they'll get
automatically destroyed after the function has left without taking
care on memory management. Now my issue is that strtok requires a non-
const char parameter to split from which I can't give. The next issue
is that I am uncertain how to fill in my const array wiht const
wchar_t values that get automatically freed up when leaving my
callback function? with strcpy and such things I'd require to create
new char arrays which I must delete by using delete[].

What I've been trying to do is to use std::wstring and its find() &
substr() functionality but i've discovered that I've got a speed loss
of ~ 30-40% (doing this for 10mio times) by using std::wstring so I
was thinking on trying out to use const wchar_t* arrays instead but I
am uncertain how to archieve my goal.

Can anyone help? Btw sorry for the possible dumb description but I am
doing a bit hard describing such things in here yet I hope it was
understandable so far.

Thanks + Regards
Alex

Oct 5 '07 #1
3 2898
On Oct 5, 2:12 pm, Alexander Adam <cont...@emiasys.comwrote:
Hi!
[snip]
How about this (untested I'm afraid). It assumes that the
input string is "AAA|BBBB" or "AAA|BBB|CCC" as you say
and does not error check otherwise:

::code begin::
void my_callback(const wchar_t* data)
{

const wchar_t* dataend = data;
while (*dataend && *dataend != L'|')
++dataend;
std::wstring ws0(data, dataend);

data = dataend;
while (*dataend && *dataend != L'|')
++dataend;
std::wstring ws1(data, dataend);

data = dataend;
while (*dataend)
++dataend;
std::wstring ws2(data, dataend);

const wchar_t* arr[3];
arr[0] = ws0.c_str();
arr[1] = ws1.c_str();
arr[2] = ws2.size() == 0 ? 0 : ws2.c_str();

my_call_to(arr);
}
::code end::

I can't see any way around copying the data if
we are to respect the constness imposed by expat.
Oct 5 '07 #2
On Oct 5, 5:24 pm, tragomaskhalos <dave.du.verg...@logicacmg.com>
wrote:
>
::code begin::
[snipped]
::code end::
Hmm, I assumed that your performance problems
with wstring were due to use of find and
substringing, hence I just used wstring as a
simple container to manage the memory, but on
reflection you may still get problems. If
performance really is still an issue, you could
try this:
- declare a biggish wchar_t array on the stack;
- copy "data" to this array wchar by wchar,
except where you see a |, put a \0 in the
array and store the index
- you'll end up with "biggish" looking something
like this:
0| 1| 2| 3| 4| 5| 6| 7| 8| 9|....
A A \0 B B B \0 C C \0
with stored indices 3 and 7;
- you can then initialise arr as
arr[0]= &biggish[0];
arr[1]= &biggish[3]; //using a var for 3, natch
arr[2]= &biggish[7]; //ditto
- BUT, during the copying, ensure you don't overrun
biggish; if you are going to, fall back to the
wstring method for this particular invokation.

If your data is fairly samey you should be able to
give biggish a size that works for most if not all
invokations.

HTH.
Oct 5 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: qwweeeit | last post by:
Splitting with RE has (for me!) misterious behaviour! I want to get the words from this string: s= 'This+(that)= a.string!!!' in a list like that considering "a.string" as a word. Python...
5
by: fatted | last post by:
I'm trying to write a function which splits a string (possibly multiple times) on a particular character and returns the strings which has been split. What I have below is kind of (oh dear!)...
4
by: JeffM | last post by:
Quick C# question: I have comma delimited values in a string array that I want to pass to seperate variables. Any tips on splitting the array? Thanks in advance! JM
2
by: Trint Smith | last post by:
Ok, My program has been formating .txt files for input into sql server and ran into a problem...the .txt is an export from an accounting package and is only supposed to contain comas (,) between...
20
by: Opettaja | last post by:
I am new to c# and I am currently trying to make a program to retrieve Battlefield 2 game stats from the gamespy servers. I have got it so I can retrieve the data but I do not know how to cut up...
2
by: CharChabil | last post by:
Using Vb.net 2005, I want to read each part in this string in an array (splitting the string) ----------- A1/EXT "BK82 LB73 21233" 105 061018 1804 ----------- That Code that i used is as follow:...
6
by: HMS Surprise | last post by:
The string s below has single and double qoutes in it. For testing I surrounded it with triple single quotes. I want to split off the portion before the first \, but my split that works with...
2
by: shadow_ | last post by:
Hi i m new at C and trying to write a parser and a string class. Basicly program will read data from file and splits it into lines then lines to words. i used strtok function for splitting data to...
4
by: yogi_bear_79 | last post by:
I have a simple string (i.e. February 27, 2008) that I need to split into three parts. The month, day, and year. Splitting into a string array would work, and I could convert day and years to...
37
by: xyz | last post by:
I have a string 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168 for example lets say for the above string 16:23:18.659343 -- time 131.188.37.230 -- srcaddress 22 ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.