473,383 Members | 1,862 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,383 software developers and data experts.

Too obfuscated?

(I want the purpose of the following code to be clear, and I'm sure
I've failed ;))

/* Assume that str is always of the form ([0-9]+[.])*[0-9]+ */

void DoStuff( const char *str )
{
const char *cp;

for ( cp=str ; cp ; cp=(cp=strchr(cp,'.'))?cp+1:0 ) {
/* do things with atoi(cp) */
}
}

Perhaps

void DoStuff( const char *str )
{
const char *cp;

cp=str;
while( cp ) {
if( cp == '.' )
cp++;
/* do things with atoi(cp) */
cp=strchr( cp, '.' );
}
}

would be better? Any even better suggestions?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #1
6 1170
nrk
Christopher Benson-Manica wrote:
(I want the purpose of the following code to be clear, and I'm sure
I've failed ;))

/* Assume that str is always of the form ([0-9]+[.])*[0-9]+ */

void DoStuff( const char *str )
{
const char *cp;

for ( cp=str ; cp ; cp=(cp=strchr(cp,'.'))?cp+1:0 ) {
/* do things with atoi(cp) */
}
}

Perhaps

void DoStuff( const char *str )
{
const char *cp;

cp=str;
while( cp ) {
if( cp == '.' )
cp++;
/* do things with atoi(cp) */
cp=strchr( cp, '.' );
}
}

would be better? Any even better suggestions?


How about:

void do_stuff(const char *str) {
char *cptr;

while ( *str ) {
long l = strtol(str, &cptr, 0); /* set base to suit your needs */
/* do stuff with l */
/* replace with *cptr == '.' if needed below */
str = *cptr ? cptr + 1 : cptr;
}
}

You do know that strtol is preferred over atoi, right? :-)

-nrk.

--
Remove devnull for email
Nov 14 '05 #2
On Mon, 2 Feb 2004, Christopher Benson-Manica wrote:
(I want the purpose of the following code to be clear, and I'm sure
I've failed ;))

/* Assume that str is always of the form ([0-9]+[.])*[0-9]+ */

void DoStuff( const char *str )
{
const char *cp;

for ( cp=str ; cp ; cp=(cp=strchr(cp,'.'))?cp+1:0 ) {
/* do things with atoi(cp) */
}
Any even better suggestions?


I'd keep it simple:

if (str) {
const char *cp=str;
for (;;) {
/* do things with atoi(cp) */
cp=strchr(cp, '.');
if (!cp) break;
cp++;
}
}

None of the codes process stuff after the last '.' yet the regexp
indicates there is some, btw.

Nov 14 '05 #3
Groovy hepcat Christopher Benson-Manica was jivin' on Mon, 2 Feb 2004
15:44:03 +0000 (UTC) in comp.lang.c.
Too obfuscated?'s a cool scene! Dig it!
(I want the purpose of the following code to be clear, and I'm sure
I've failed ;))

/* Assume that str is always of the form ([0-9]+[.])*[0-9]+ */

void DoStuff( const char *str )
{
const char *cp;

for ( cp=str ; cp ; cp=(cp=strchr(cp,'.'))?cp+1:0 ) {
/* do things with atoi(cp) */
}
}
Ick!
Perhaps

void DoStuff( const char *str )
{
const char *cp;

cp=str;
while( cp ) {
if( cp == '.' ) ^
You're missing a dereference here.
cp++;
/* do things with atoi(cp) */
cp=strchr( cp, '.' );
}
}

would be better? Any even better suggestions?


That's better, but still not great. You're effectively testing the
same value twice (strchr(cp, '.') followed by if(*cp == '.') in the
next iteration). A better approach would be to call strchr() in the
controlling expression of the loop, like so:

void DoStuff(const char *str)
{
while(NULL != (str = strchr(str, '.')))
{
/* do yer thing with str */
}
}

If you find that unclear, you might prefer to do it this way (it
amounts to the same thing):

void DoStuff(const char *str)
{
str = strchr(str, '.');
while(str)
{
/* do yer thing with str */
str = strchr(str, '.');
}
}

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 14 '05 #4
Peter Shaggy Haywood wrote:
.... snip ...
void DoStuff(const char *str)
{
str = strchr(str, '.');
while(str)
{
/* do yer thing with str */
str = strchr(str, '.');
}
}


That misses the essential characteristic of starting after the
'.'. He might try:

void DoStuff(const char *str)
{
while (str = strchr(str, '.') { /* found a period */
str++; /* skip over it */
/* do yer thing with str */
}
}

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #5
Peter "Shaggy" Haywood <sh****@australis.net.stop.spam> spoke thus:
for ( cp=str ; cp ; cp=(cp=strchr(cp,'.'))?cp+1:0 ) {
/* do things with atoi(cp) */
}
Ick!
Mikey likes it!! ;)
if( cp == '.' )

^ You're missing a dereference here.


Thank you.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #6
CBFalconer <cb********@yahoo.com> spoke thus:
void DoStuff(const char *str)
{
while (str = strchr(str, '.') { /* found a period */
str++; /* skip over it */
/* do yer thing with str */
}
}


That I like. Although I like to think my original implementation had
a certain twisted charm to it.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #7

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

Similar topics

4
by: fix | last post by:
Hello, i need to protect my xsl file. In my xsl i have a mechanism for defining a personal stylesheet, what i want to do is, if it possible, find a mechanism for Obfuscated the xsl source code ....
1
by: Wibo | last post by:
Hello, we have a large application, that gave weird behavior. On W2K we're suffering from instable program ececution from time to time, untill now we managed to workaround these problems. On...
1
by: assaf | last post by:
hi all i am using PreEmptive Solutions dotfuscator (community edition). and i am getting 'TypeLoadException' for a simple interface that i defined. how can i debug an obfuscated application? ...
0
by: Steve Covert | last post by:
Has anyone encountered problems with building an installer for a Windows app using Wise? In my case, the installation fails due an obfuscated dll. Instructions are provided for remedying this from...
58
by: Jeff_Relf | last post by:
Hi Tom, You showed: << private const string PHONE_LIST = "495.1000__424.1111___(206)564-5555_1.800.325.3333"; static void Main( string args ) { foreach (string phoneNumber in Regex.Split...
5
by: Thomas Garai | last post by:
Hi, Does anybody know the details of the obfuscated C contest that was around a couple of years ago? I have a couple of pearlers I would like to enter.... hehehe :) cheers
0
by: daechulsohn | last post by:
Just thought I would contribute some knowledge for a change. After hours tracking down a problem with obfuscated code, where we were getting the exception : System.TypeLoadException: Method a...
4
by: Paul E Collins | last post by:
For those who appreciate such things, here's a bit of obfuscated C# I knocked up in a spare moment at work. http://CL4.org/comp/jabberwocky.txt P. -- www.CL4.org
35
by: +-={K-SoL}=-+ | last post by:
when I try to reference 2 obfuscated libraries at the same time visual studio gives me a compilation error since the obfuscator has converted some name spaces to a and b in both libraries, now I...
4
by: Bry | last post by:
I'm trying Obfuscation for the first time using the community edition of dotfuscator that ships with vs .net 2005. After building my code, I load the compiled .exe into dotfuscator and let it...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.