Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7594883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:31:54+00:00 2026-05-30T21:31:54+00:00

I was wondering could someone please help me implemenrt this method whereas I dont

  • 0

I was wondering could someone please help me implemenrt this method whereas I dont need to use std::string.

The method takes in tewo parameters, one for char array and the other one is the size of the char array.

The parameters passed will be an array of differernt values seperated by commas, e.g. “Rule1,Rule2,Rule3,Rule4,AT,T,Cat,Dog”.

The member variable “m_rulesSet” is a std::string constaining simular values. I want to compare the both to check if the “name” is within the std::string “m_ruleSet”

bool 
Matche(const char *str, size_t strSize)
{
    std::string target(str, strSize);

    if(m_ruleSet.empty())
    {
        return true;
    }
    if((NULL == str) || (strSize <= 0))
    {
        return false;
    }

    const char * ptr =0;
    const char * start = target.c_str();

    while ((ptr = strchr(start, ',')) != 0)
    {
        std::string name(start, ptr - start);
        if(name ==m_ruleSet)
        {
            return true;
        }
        start = ptr + 1;
    }
    if(*start)
    {
        std::string name(start);
        if(name==m_ruleSet)
        {
            return true;
        }
    }
    return false;
}

Please any help would be appreciated, thank you so much in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:31:55+00:00Added an answer on May 30, 2026 at 9:31 pm

    So you simply mean replacing std::string comparison by strcmp or strncmp (as this is the only thing you use the std::string for, everything else is already using C-strings)? Well, if you really want it:

    bool Matche(const char *str, size_t strSize)
    {
        if(m_ruleSet.empty())
            return true;
        if(!str || !strSize)     //remember that size_t is unsigned by standard
            return false;
    
        const char * ptr, start = str;
        while (ptr = memchr(start, ',', strSize))
        {
            size_t len = ptr - start;
            if(len == m_ruleSet.size() && !strncmp(start, m_ruleSet.c_str(), len))
                return true;
            strSize -= len + 1;
            start = ptr + 1;
        }
        return *start ? (strSize == m_ruleSet.size() && 
                         !strncmp(start, m_ruleSet.c_str(), strSize)) : false;
    }
    

    Like Ben points out in his comment and answer, you need to use memchr instead of strchr when you got an additional size argument and your string is not neccessarily zero-terminated.

    You may also want to replace m_ruleSet by a C-string (to get rid of the .c_str()), but I actually question the need for replacing std::string by C-strings in C++ code in the first place.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if someone could please help me with this: printf(Enter path for
was wondering if someone could help with this one. This is the first time
I was wondering if someone could help me understand this problem. I prepared a
I am wondering if someone could please help me convert a piece of PHP
I was wondering if someone could help me simplify my current counting method. I´m
Hey guys I was wondering if someone could provide a little help. I've been
I ran across this and was wondering if someone could explain why this works
I saw this tip in another question and was wondering if someone could explain
please could someone help me please as have just started using actionscript and really
I'm wondering if someone can help me with this. I have three TabPanels depending

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.