7 Replies - 2724 Views - Last Post: 17 December 2011 - 05:13 PM

#1 thephpdev  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 59
  • Joined: 01-July 11

C++ PHP Function Library

Posted 17 November 2011 - 08:41 PM

I created this little library to help programmers handle text processing in C++. It holds some simple functions like:
# str_replace(del, rep, str); Replace DEL with REP in STR
# strpos(str, del); Gets first occurance of DEL in STR
# strrev(str); Reverses string
# strtoupper(str); Makes all characters in string uppercase
# strtolower(str); Makes all characters in string lowercase
# strlen(str); Gets length of string
# ord(char); Returns ascii of character
# ucfirst(str); Makes first character uppercase in STR
# ucwords(str); Makes all first characters of words uppercase in STR
# explode(del, str); Returns vector <string> with strings from STR (STR seperated by DEL)
# implode(e); Changes vector <string> combines strings with no delimiter
# implode(glue, e); Changes vector <string> into a string with seperater GLUE
# inttostr(a); Changes A into a string
# strtoint(str); Changes STR into an int


This is all I have so far. Although I would like to make more, so give any suggestions on the functions. I know this is double-posting but I think more use will come out of it here.

The download website is here: c++php

Thanks for reading, and I hope you will like what I've got.

This post has been edited by thephpdev: 17 November 2011 - 08:49 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ PHP Function Library

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,377
  • Joined: 31-December 10

Re: C++ PHP Function Library

Posted 17 November 2011 - 08:58 PM

You should try to pass strings by const-reference to eliminate unnecessary copies, especially if you're not modifying the string in the function. I would also recommend looking into <algorithm> and <iterator> because you're duplicating functions that have already been done.

This post has been edited by vividexstance: 17 November 2011 - 08:58 PM

Was This Post Helpful? 0
  • +
  • -

#3 thephpdev  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 59
  • Joined: 01-July 11

Re: C++ PHP Function Library

Posted 17 November 2011 - 09:21 PM

View Postvividexstance, on 17 November 2011 - 09:58 PM, said:

You should try to pass strings by const-reference to eliminate unnecessary copies, especially if you're not modifying the string in the function. I would also recommend looking into <algorithm> and <iterator> because you're duplicating functions that have already been done.

I made the const-reference changes in str_replace, strtoupper, strtolower, ucfirst, ucwords, and explode (2nd version). As for the <algorithm> and <iterator> comment, that is good to hear. Although moving forward, if anyone has their own function they would like to send or a function idea I'll happily add that.
Was This Post Helpful? 0
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,377
  • Joined: 31-December 10

Re: C++ PHP Function Library

Posted 18 November 2011 - 10:17 AM

Here's a good example of using the algorithms and iterators to solve your problem for the strrev():
string strrev(const string& str)
{
    string result;
    copy(str.rbegin(), str.rend(), back_inserter(result));
    return result;
}


Was This Post Helpful? 0
  • +
  • -

#5 RetardedGenius  Icon User is offline

  • >>──(Knee)──►
  • member icon

Reputation: 126
  • View blog
  • Posts: 555
  • Joined: 30-October 10

Re: C++ PHP Function Library

Posted 12 December 2011 - 01:07 PM

I think that your str_replace(), strpos() and strrev() implementations are inefficient. I would recommend taking a look at C++'s own string class; it provides most of the functions you've created anyway...

This post has been edited by RetardedGenius: 17 December 2011 - 12:00 PM

Was This Post Helpful? 0
  • +
  • -

#6 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1623
  • View blog
  • Posts: 5,710
  • Joined: 03-August 09

Re: C++ PHP Function Library

Posted 13 December 2011 - 02:33 PM

View Postvividexstance, on 18 November 2011 - 05:17 PM, said:

Here's a good example of using the algorithms and iterators to solve your problem for the strrev():
string strrev(const string& str)
{
    string result;
    copy(str.rbegin(), str.rend(), back_inserter(result));
    return result;
}



or better yet...

std::string strrev(const std::string& s) {
    return std::string(s.rbegin(), s.rend());
}



you could actually just manually inline that, or if you don't need a copy, just use std::reverse.

edit:
lol, there is also std::reverse_copy :) it works just like using std::copy if you pass reverse iterators.

This post has been edited by ishkabible: 13 December 2011 - 02:36 PM

Was This Post Helpful? 0
  • +
  • -

#7 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,377
  • Joined: 31-December 10

Re: C++ PHP Function Library

Posted 14 December 2011 - 09:58 AM

I was just showing a simple example to the OP because it seemed like he/she didn't know about the algorithm and iterator headers. Your function is obviously shorter, but I wanted to show how the std::back_inserter worked.
Was This Post Helpful? 0
  • +
  • -

#8 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 451
  • View blog
  • Posts: 855
  • Joined: 17-March 11

Re: C++ PHP Function Library

Posted 17 December 2011 - 05:13 PM

Boost string library is probably worth mentioning too:

http://www.boost.org...tring_algo.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1