473,667 Members | 2,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Splitting a string

Hi!

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

void my_callback(con st wchar_t* data);

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

void my_call_to(cons t 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 2932
On Oct 5, 2:12 pm, Alexander Adam <cont...@emiasy s.comwrote:
Hi!
[snip]
How about this (untested I'm afraid). It assumes that the
input string is "AAA|BBBB" or "AAA|BBB|CC C" as you say
and does not error check otherwise:

::code begin::
void my_callback(con st 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
1918
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 2.3.4 (#2, Aug 19 2004, 15:49:40) on linux2
5
2959
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!) printing the results I expect, which I guess means my dynamic memory allocation is a mess. Also, I was advised previously that I should really free memory in the same place I declare it, but I'm not sure how I would go about doing this in my code...
4
2012
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
2514
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 fields in a table...well, someone has been entering description fields with comas (,) in the description and now it is splitting between one field...example: "santa clause mushrooms, pens, cups and dolls" I somehow need to NOT split anything...
20
3693
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 the data to assign each value to its own variable. So right now I am just saving the data to a txt file and when I look in the text file all the data is there. Not sure if this matters but when I open the text file in Word pad (Rich Text) It...
2
1481
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: Dim s As String, h As String Dim delim(1) As Char delim(0) = "/"
6
303
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 shorter strings does not seem to work with this one. Ideas? Thanks, jvh
2
3264
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 lines it worked quite well but srttok isnot working for multiple blank or commas. Can strtok do this kind of splitting if it cant what should i use . Unal
4
2820
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 integers later. I've bene looking around, and everything I see seems more complicated than it should be! Help!
37
1835
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 --srcport 131.188.37.59 --destaddress 1398 --destport tcp --protocol
0
8366
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8790
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8565
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8650
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7391
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2779
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2017
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.