CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Dec 2005
    Posts
    114

    Need a text class.

    I require a class with top of the line text manipulation. I am not refering to a string class, something which is terminated by a null character, but a text class. The size of the text would not be a property dependent upon the location of a null character but rather the amount of space allocated for the text. In Visual Basic, this is called a string.

    So can someone help me to find a good text manipulation class with all the bells and whistles?

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Need a text class.

    Quote Originally Posted by Guidosoft
    I am not refering to a string class, something which is terminated by a null character, but a text class.
    The std::string class isn't terminated by a null character, it's terminated by length. It's only when you use it with C string functions that the NULL character becomes a problem.

    - petter

  3. #3
    Join Date
    Dec 2005
    Posts
    114

    Re: Need a text class.

    Is there a way that I can put null characters in the string and retrieve the data in a buffer?

  4. #4
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Need a text class.

    You can do it like this:
    Code:
    //create string with lots of nulls
    std::string str("It's\0 too\0 late\0.", 18);
    
    //create a buffer and copy all data
    char* buf = new char[str.length()];
    memcpy(buf, str.c_str(), str.length());
    
    // clean up
    delete[] buf;
    - petter

  5. #5
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: Need a text class.

    in Above code also if you check the value of str it will show only it's not Full statement this will also terminate on NULL .

    Thanx

  6. #6
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Need a text class.

    Quote Originally Posted by humptydumpty
    in Above code also if you check the value of str it will show only it's not Full statement this will also terminate on NULL.
    What do you mean by 'check the value'? Internally this string should contain 18 characters (including zeros).

    - petter

  7. #7
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: Need a text class.

    peter i mean to say if we perform Initialization of string in following manner it will Show Only those character which appear before First Occurance of NULL
    Code:
    std::string str("it's\0 hello\0",18);
    std::cout<<strlen(str.c_str());//here it's Will Show 4 Only
    same way if i use a Pointer

    Code:
    char *str = new char[255]; //here you allocated space for 255 character
    strcpy(str,"it's\0 hello");
    std::cout<<str; //output will be it's Only
    //and i tried
    So we can See all Terminate at NULL condition only not on the basis of space allocation.That's what i want to say .what i feel from OP post.this is the answer of that question.

    so here if you want to print full One on the basis of space you have to use escape character


    Code:
    std::string str("It's\\0 too\\0 late\\0");
     std::cout<<str.c_str();//and out put will be It's\\0 too\\0 late\\0
     std::cout<<strlen(str.c_str());//here it's Will Show 19 Only
    Thanx
    Last edited by humptydumpty; August 26th, 2006 at 04:53 AM.

  8. #8
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Need a text class.

    Quote Originally Posted by humptydumpty
    strcpy(str,"it's\0 hello");
    strcpy is a c-string function and will terminate at the first occurance of a \0. But that doesn't apply to the std::string class:

    Code:
    //create string with lots of nulls
    std::string str("It's\0 too\0 late\0.", 18);
    
    //create a buffer and copy all data
    char* buf = new char[str.length()];
    memcpy(buf, str.c_str(), str.length()); // I don't use strcpy here
    
    // print out all characters in 'buf'
    for (int i = 0; i < str.length(); ++i)
        printf("%x - %c\n", buf[i], buf[i]);
    
    // clean up
    delete[] buf;
    This should print out a list of all characters in str (after they've been copied to buf):
    Code:
    49 - I
    74 - t
    27 - '
    73 - s
    0  -
    20 -
    74 - t
    6f - o
    6f - o
    0  -
    20 -
    6c - l
    61 - a
    74 - t
    65 - e
    0  -
    2e - .
    0  -
    - petter

  9. #9
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Need a text class.

    Quote Originally Posted by wildfrog
    You can do it like this:
    Code:
    //create string with lots of nulls
    std::string str("It's\0 too\0 late\0.", 18);
    
    //create a buffer and copy all data
    char* buf = new char[str.length()];
    memcpy(buf, str.c_str(), str.length());
    
    // clean up
    delete[] buf;
    - petter
    You've got a couple of problems there:
    1. There's no guarantee that string::c_str() returns a pointer to string's own buffer - it's allowed to create a new buffer and only fill it with the chars up to the first '\0;
    2. Even if it did, there's no guarantee that the internal buffer occupies contiguous storage.
    In practice, I can't see number 1 being too much of a problem (it could run into problems with ownership of the new buffer), but number 2 is potentially a killer.

    However, you're correct that string, properly used (i.e. use c_str() sparingly and only when really needed), seems to meet the OP's requirements.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  10. #10
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: Need a text class.

    here is a valid Solution for That
    Code:
    std::string str("It's\\0 too\\0 late\\0.");
    std::cout<<str.c_str();//and out put will be It's\\0 too\\0 late\\0
    std::cout<<std::endl;
    char* buf = new char[str.length()+1];
    strcpy(buf,str.c_str());
    std::cout<<str.c_str();
    delete[] buf;
    Thanx
    Last edited by humptydumpty; August 26th, 2006 at 06:09 AM.

  11. #11
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Need a text class.

    Quote Originally Posted by Graham
    You've got a couple of problems there:
    1. There's no guarantee that string::c_str() returns a pointer to string's own buffer - it's allowed to create a new buffer and only fill it with the chars up to the first '\0;
    2. Even if it did, there's no guarantee that the internal buffer occupies contiguous storage.
    In practice, I can't see number 1 being too much of a problem (it could run into problems with ownership of the new buffer), but number 2 is potentially a killer.
    You're right about that, and the moral is that when working with std::string don't treat then like they're c-string... because they're not.

    Maybe a solution would be to use std::copy:
    Code:
    std::copy(str.begin(), str.end(), buf);
    - petter

  12. #12
    Join Date
    Dec 2005
    Posts
    114

    Re: Need a text class.

    What about string:ata()? What does that do?

  13. #13
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Need a text class.

    Quote Originally Posted by Guidosoft
    What about string:ata()? What does that do?
    I guess its the same except that data does not have traits::eos() appended as does c_str() has. So, data() returns size() number of elements while c_str() returns size() + 1 elements.

  14. #14
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    Re: Need a text class.

    Quote Originally Posted by Graham
    You've got a couple of problems there:
    1. There's no guarantee that string::c_str() returns a pointer to string's own buffer - it's allowed to create a new buffer and only fill it with the chars up to the first '\0;
    2. Even if it did, there's no guarantee that the internal buffer occupies contiguous storage.
    In practice, I can't see number 1 being too much of a problem (it could run into problems with ownership of the new buffer), but number 2 is potentially a killer.

    However, you're correct that string, properly used (i.e. use c_str() sparingly and only when really needed), seems to meet the OP's requirements.
    Hum, no, c_str() always returns the full data held by the string class, including embedded nulls. So wildfrog's code is perfectly fine.

    Quote Originally Posted by 21.3.6.1
    const charT* c_str() const;
    Returns: A pointer to the initial element of an array of length size() + 1 whose first size() elements equal the corresponding elements of the string controlled by *this and whose last element is a null character specified by charT().
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  15. #15
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Need a text class.

    Ah, OK, I missed that bit. Still not guaranteed to be the same memory area as used by the string, though, so the rest applies.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured