473,395 Members | 2,436 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,395 software developers and data experts.

c++ code snippets site

Hi

I'd like to place this code to C++ code snippets site.
Does anyone know about where is could be?

Andrei Smirnov

=======================================
//
// example of how to strip std C++ string of empty symbols
// in header and trailer
//

#include <iostream>
#include <string>
using namespace std;

void string_strip(string* s)
{
static const char* empties = " \t\n\r";
int b = s->find_first_not_of(empties);
if (b == -1) {
*s = string();
return;
}
int e = s->find_last_not_of(empties);
*s = string(*s, b, e + 1 - b);
}

int main()
{
string ss[] = {
" Hi my friend\t \n",
"",
" \t hi \n\t\r",
"HI ",
"SSSSSS",
" Bye"
};
for (int i = 0; i < sizeof(ss) / sizeof(ss[0]); ++i) {
string s = ss[i];
string_strip(&s);
cerr << "'" << s << "'" << endl;
}
}

Jul 22 '05 #1
8 2334
as****************@yahoo.com wrote:
I'd like to place this code to C++ code snippets site.
Why? You posted it here, it should soon be available on the Google
newsgroup archives site...
[...]

Jul 22 '05 #2
<as****************@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi

I'd like to place this code to C++ code snippets site.
Why? What does it do that you think others would
find useful? It's just plain wrong.
Does anyone know about where is could be?
I'm not sure which site you mean. I do know of one called
www.snippets.org

You can visit there to find out if/how code submissions
are done.

=======================================
//
// example of how to strip std C++ string of empty symbols
// in header and trailer
//

#include <iostream>
#include <string>
using namespace std;

void string_strip(string* s)
Why a pointer? Why not a reference?
{
static const char* empties = " \t\n\r";
int b = s->find_first_not_of(empties);
The return type of 'std::string::find_first_not_of()' is
'std::string::size_type', not 'int'. This is the case
for all the string's 'find' functions.
if (b == -1) {
Why are you checking for -1. If 'find_first_not_of()' does
not find anything, it returns the value 'std::string::npos',
whose value is implementation defined. It could be -1, it
could be some other value.
*s = string();
return;
}
int e = s->find_last_not_of(empties);
Again, wrong type being used to store return value.
*s = string(*s, b, e + 1 - b);
}

int main()
{
string ss[] = {
" Hi my friend\t \n",
"",
" \t hi \n\t\r",
"HI ",
"SSSSSS",
" Bye"
};
Why use an array when you have the much safer and easier
to use containers, e.g. vector?
for (int i = 0; i < sizeof(ss) / sizeof(ss[0]); ++i) {
string s = ss[i];
string_strip(&s);
cerr << "'" << s << "'" << endl;
}
}


I very much doubt the above code would be accepted by a
site offering code snippets. It's got many problems.

If you scan the archives of this group, you'll find several
much superior solutions than yours.

-Mike
Jul 22 '05 #3
Mike Wahler wrote:
<as****************@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I'd like to place this code to C++ code snippets site.
Why? What does it do that you think others would
find useful? It's just plain wrong.


it has some features to tease geeks like you :)
Does anyone know about where is could be?


I'm not sure which site you mean. I do know of one called
www.snippets.org


this is useless crap indeed. my favorite was 'portable string class' :)
=======================================
//
// example of how to strip std C++ string of empty symbols
// in header and trailer
//

#include <iostream>
#include <string>
using namespace std;

void string_strip(string* s)


Why a pointer? Why not a reference?


not of you business. i like pointers, there were here before stroustrup
decided to win battle with fortran and introduced refs solely to
support
operations overloading (unfortunately this created good breed
environment
for people who tread ANSI C++ standard as bible).
{
static const char* empties = " \t\n\r";
int b = s->find_first_not_of(empties);
The return type of 'std::string::find_first_not_of()' is
'std::string::size_type', not 'int'. This is the case
for all the string's 'find' functions.


could you give me the list of compilers/platforms where is matters?
if (b == -1) {
Why are you checking for -1. If 'find_first_not_of()' does
not find anything, it returns the value 'std::string::npos',
whose value is implementation defined. It could be -1, it
could be some other value.


could you imagine any other value than -1. think hard...
.....

int main()
{
string ss[] = {
" Hi my friend\t \n",
"",
" \t hi \n\t\r",
"HI ",
"SSSSSS",
" Bye"
};


Why use an array when you have the much safer and easier
to use containers, e.g. vector?


because i like everything unsafe and hard. this is my choice.
for (int i = 0; i < sizeof(ss) / sizeof(ss[0]); ++i) {
string s = ss[i];
string_strip(&s);
cerr << "'" << s << "'" << endl;
}
}

I very much doubt the above code would be accepted by a
site offering code snippets. It's got many problems.

it was accepted by usenet already without asking you.
If you scan the archives of this group, you'll find several
much superior solutions than yours.
could you be more specific? i just want the function which strips
spaces
out of my string. i don't need superior solution of this apparently
'very
big' problem.

-Mike


Andrei

PS: if you are so cool with standard can you tell me what virse 2 of
12.1.5
said. you've got thirty seconds, otherwise you are not cool. time is
running...

Jul 22 '05 #4
that was precisely the idea

Andrei

Jul 22 '05 #5
as****************@yahoo.com wrote:
[... insulting and rude response snipped ...]


Perhaps you could be a bit less "cool" and a bit more tactful.
Just a thought. More like dream, probably.

Spend more time lurking around instead of suggesting "solutions"
and insulting regulars, and perhaps everybody is going to have
even more pleasant time visiting comp.lang.c++.
Jul 22 '05 #6

<as****************@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Mike Wahler wrote:
<as****************@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I'd like to place this code to C++ code snippets site.
Why? What does it do that you think others would
find useful? It's just plain wrong.


it has some features to tease geeks like you :)


Which are?
Does anyone know about where is could be?
I'm not sure which site you mean. I do know of one called
www.snippets.org


this is useless crap indeed.

Usefulness is in the eye of the beholder. I for one, have
found some of the code there useful for learning purposes.
If others have found it useless, that doesn't change what
it's done for me.
my favorite was 'portable string class' :)
=======================================
//
// example of how to strip std C++ string of empty symbols
// in header and trailer
//

#include <iostream>
#include <string>
using namespace std;

void string_strip(string* s)
Why a pointer? Why not a reference?


not of you business.
i like pointers,


Why? IMO they are one of the major sources of bugs.
there were here before stroustrup
decided
So you seem to be basing your claim of their superiority
over references upon their tenure. I find that to be
very poor logic. Abacuses have been around for thousands
of years. Are they superior to the modern digital computer?
to win battle with fortran
Perhaps you should read some of Stroustrup's writings (e.g.
his "Design and Evolution of C++") before you try to put words
into his mouth.
and introduced refs solely to
support
operations overloading
Repeat my last paragraph above.
(unfortunately this created good breed
environment
for people who tread ANSI C++ standard as bible).
I treat the C++ standard for what it is: the single authoritative
definition of the C++ language. I exploit the guarantees it
grants to me regarding my C++ programs, when translated by
implementations which adhere to it.

{
static const char* empties = " \t\n\r";
int b = s->find_first_not_of(empties);


The return type of 'std::string::find_first_not_of()' is
'std::string::size_type', not 'int'. This is the case
for all the string's 'find' functions.


could you give me the list of compilers/platforms where is matters?


No need. I have the language standard to guarantee behavior
(if I follow the rules.) One such rule is that the 'std::string'
'find' functions report 'not found' with a value denoted by
the expression 'std::string::npos'. No other value is given
this guarantee, nor is there any guranteee of a particular
actual value for 'npos'.
if (b == -1) {


Why are you checking for -1. If 'find_first_not_of()' does
not find anything, it returns the value 'std::string::npos',
whose value is implementation defined. It could be -1, it
could be some other value.


could you imagine any other value than -1. think hard...


Any negative value, or any positive value outside the
range of 'std::string::size_type'.

....

int main()
{
string ss[] = {
" Hi my friend\t \n",
"",
" \t hi \n\t\r",
"HI ",
"SSSSSS",
" Bye"
};
Why use an array when you have the much safer and easier
to use containers, e.g. vector?


because i like everything unsafe and hard. this is my choice.


So be it. I'll add your name to my 'no-hire' list. :-)
for (int i = 0; i < sizeof(ss) / sizeof(ss[0]); ++i) {
string s = ss[i];
string_strip(&s);
cerr << "'" << s << "'" << endl;
}
}

I very much doubt the above code would be accepted by a
site offering code snippets. It's got many problems.


it was accepted by usenet already without asking you.


Usenet is not a person which can make decisions or 'accept'
or 'reject' something. It's just a 'place'. A tree will
'accept' me shooting it with a shotgun, that doesn't stop
such an action from being detrimental to the tree.
If you scan the archives of this group, you'll find several
much superior solutions than yours.


could you be more specific?


Um, no I won't do your research for you. I've already pointed
out a promising place for you to look.
i just want the function which strips
spaces
out of my string.
And there are many which have already been posted to
Usenet (and the web) many times.
i don't need superior solution
Yet you claim to have created one, but have failed
to demonstrate its superiority.
of this apparently
'very
big' problem.
I don't find it a big problem at all. I've long ago solved
it, in many different languages.
-Mike


Andrei

PS: if you are so cool with standard


I'm not, and never have claimed to be 'cool with standard'.
However I do possess a copy of the ISO standard for C++, and
refer to it when I need to know a conclusive answer to a
question about C++.
can you tell me what virse 2 of
12.1.5
said.
That one's easy. It doesn't say anything, because it does not exist.
you've got thirty seconds,
I'll take as much time as I need, thank you, until such time
as you're paying me to do things for you.
otherwise you are not cool.
My goal is not and never has been to be 'cool'.
time is
running...


Always has been, always will. So what?
OK, so you're 'cool'. I'm productive. That's the way I like it.

-Mike
Jul 22 '05 #7
Mike Wahler wrote:

OK, so you're 'cool'. I'm productive. That's the way I like it.


Gotta add that to my .sig. Well, when I've got a .sig again...

:-)
Jul 22 '05 #8
Hi Andrei,
you could always start a project on www.codeproject.com or
www.developersex.com if you feel the need to share what you have
written with the world..:-)

If you actually want to build something significant and have other
people contribute you could start an open source project on
www.sourceforge.net.

Best Regards

Peter Nolan

Jul 22 '05 #9

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

Similar topics

2
by: juglesh | last post by:
howdy, I am using this code library thing: http://sourceforge.net/projects/php-csl/ http://www.php-csl.com/snippets/ and had an idea. I was thinking about keeping a bunch of functions in the...
2
by: - Steve - | last post by:
I'm working on my asp.net site and I'm wondering what the best way to reuse little snippets of code is? Right now I have a Class in it's own cs file that I call Snippets. Then when I want to...
18
by: Joe Fallon | last post by:
I have some complex logic which is fairly simply to build up into a string. I needed a way to Eval this string and return a Boolean result. This code works fine to achieve that goal. My...
19
by: Swaregirl | last post by:
Hello, I would like to build a website using ASP.NET. I would like website visitors to be able to download code that I would like to make available to them and that would be residing on my...
13
by: frk.won | last post by:
I am interested in learning how to use the VS 2005 code snippets. However, I wish to know what are the best ways to source control the code snippets? Are there any source safe/subversion...
2
by: Wikicodia Admin | last post by:
Dears, Wikicodia is a wiki based project for sharing code snippets. We're collecting large number of code snippets for all code-based programming languages, scripts, shells and consoles. We wish...
1
by: StreamLogic | last post by:
All, I have posted this question in various groups, so I apologize for any overlap. I'd like to hear how other developers/architects keep track of old scripts, code snippets, relevant URLs,...
2
Frinavale
by: Frinavale | last post by:
I'm attempting to supplement the help for my code by providing other developers with code snippets that demonstrate how to use my classes/method. I've created a .snippet file and have placed it in...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...

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.