Giter VIP home page Giter VIP logo

stringspliter's Introduction

StringSpliter

C++ class to split a string into pieces by given delimiters, a wrapper of c_tokenizer.

string_spliter.h

/* string_spliter.h */

#pragma once
#ifndef STRING_SPLITER_H
#define STRING_SPLITER_H

class StringSpliter {
public:
    enum {EMPTIES_OK = 0, NO_EMPTIES};

public:
    StringSpliter(const char* str, const char* delimiters, int empties = StringSpliter::EMPTIES_OK);
    ~StringSpliter();
    const char* Next();

private:
    char*       m_string;
    const char* m_delimiters;
    char*       m_current;
    char*       m_next;
    bool        m_is_ignore_empties;
};

#endif

string_spliter.cpp

/* string_spliter.cpp */

#include <string>
#include "string_spliter.h"

StringSpliter::StringSpliter(const char* str, const char* delimiters, int empties)
{
    if (str && delimiters) {
        size_t n = strlen(str) + 1;
        char  *p = (char*)malloc(n);
        if (p) {
            this->m_string = (char*)memcpy(p, str, n);
        } else {
            this->m_string = NULL;
        }
    } else {
        this->m_string = NULL;
    }
    this->m_delimiters        = delimiters;
    this->m_current           = NULL;
    this->m_next              = this->m_string;
    this->m_is_ignore_empties = (empties != StringSpliter::EMPTIES_OK);
}

StringSpliter::~StringSpliter()
{
    free(this->m_string);
    this->m_string = NULL;
}

const char* StringSpliter::Next()
{
    if (!this->m_string || !this->m_next) {
        return NULL;
    }

    this->m_current = this->m_next;
    this->m_next = strpbrk(this->m_current, this->m_delimiters);

    if (this->m_next) {
        *(this->m_next) = '\0';
        this->m_next += 1;
        if (this->m_is_ignore_empties) {
            this->m_next += strspn(this->m_next, this->m_delimiters);
            if (!(*(this->m_current))) {
                return this->Next();
            }
        }
    } else if (this->m_is_ignore_empties && !(*(this->m_current))) {
        return NULL;
    }

    return this->m_current;
}

Usage

char *str = "$PTNL,PJK,102823.80,012613,+3541087.713,N,+1878282.023,E,1,16,0.8,EHT+52.668,M*4F";
StringSpliter string_spliter(str, ",");
int n;
const char *sec;
for (n=0, sec = string_spliter.Next(); sec; sec=string_spliter.Next(), n++) {
    printf("%2d: %s\n", n, sec);
}

stringspliter's People

Contributors

allidylls avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.