• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
  • entries
    8
  • comments
    8
  • views
    11707

Some nice C++ string functions

Sign in to follow this  
Followers 0
Kazade

355 views

I just thought I'd post some code that has built up over the last few days as I have been working on my game framework. Below is a load of cool little string methods which act/look like the Python string methods. There may be some bugs but they seem to work OK. Let me know what you think...


Usage:

string s = "Hello world";
vector words = strings::split(s);
//words now contains "Hello" and "world"

string s = "1.0";
float f = strings::toFloat(s);
//f now holds 1.0

string s = "#some comment";
if (strings::startsWith(s, "#")) {
continue; //This if statement will be entered
}

etc.



strings.h

#ifndef STRING_H_INCLUDED
#define STRING_H_INCLUDED

#include
#include

using std::string;
using std::vector;

namespace strings {

bool startsWith(const string& s, const string& s2);
bool endsWith(const string& s, const string& s2);
vector split(string str, string delim = " ");
void ljust(string& s, uint length, char padChar = ' ');
void rjust(string& s, uint length, char padChar = ' ');
string strip(const string& s);
void replace(string& s, const string& find, const string& replace);

float toFloat(const string& s);
int toInt(const string& s);

int findCountOf(const string& s, const string& word);
}

#endif // STRING_H_INCLUDED



strings.cpp

#include
#include
#include
#include

using std::string;
using std::vector;
using std::istringstream;

namespace strings {
/**
Returns true if s starts with the string s2
*/

bool startsWith(const string& s, const string& s2) {
return s.find(s2) == 0;
}

/**
Returns true if s ends with the string s2
*/

bool endsWith(const string& s, const string& s2) {
return s.find_last_of(s2) == (s.length() - s2.length());
}

/**
Splits a string on the delimeter and returns each part in a
vector.
*/

vector split(string str, string delim = " ") {
string::size_type cutAt;
vector results;
while( (cutAt = str.find_first_of(delim)) != string::npos ) {
if(cutAt > 0) {
results.push_back(str.substr(0,cutAt));
}
str = str.substr(cutAt+1);
}
if (str.length() > 0) {
results.push_back(str);
}

return results;
}

/**
Adds padding characters to the left of a string until
the string is a certain length. If the string is longer
than the length passed then the string is left unchanged
*/

void ljust(string& s, uint length, char padChar = ' ') {
std::string::difference_type whitespace = length - s.size();

if (whitespace > 0)
s = s + string (whitespace, padChar);

return s;
}

void rjust(string& s, uint length, char padChar = ' ') {

std::string::difference_type whitespace = length - s.size();

if (whitespace > 0)
s = string (whitespace, padChar) + s;

}

/**
Strips spaces from the beginning and end of a string
*/

string strip(const string& s) {
string sep = " ";
const std::string::size_type first = s.find_first_not_of(sep);
return (first == std::string::npos) ? s : s.substr(first, s.find_last_not_of(sep) - first + 1);
}

/**
Replace occurrances of 'find' with 'replace' in 's'
*/

void replace(string& s, const string& find, const string& replace) {
string::size_type j;

for (; (j = s.find(find)) != string::npos;) {
s.replace(j, find.length(), replace);
}
}

/**
Attempts a type-safe cast from string to float, throws
a std::runtime_error on failure
*/

float toFloat(const string& s) {
float result;

istringstream iss(s);

if ((iss >> std::dec >> result).fail()) {
throw std::runtime_error("Error converting string to float");
}

return result;
}

/**
Attempts a type-safe cast from string to integer, throws
a std::runtime_error on failure
*/

int toInt(const string& s) {
int result;

istringstream iss(s);

if ((iss >> std::dec >> result).fail()) {
throw std::runtime_error("Error converting string to integer");
}

return result;
}


/**
Find the number of occurances of a word in a string (s)
*/

int findCountOf(const string& s, const string& word) {
int count = 0;
string::size_type wordPos = 0;

while (wordPos != string::npos) {
wordPos = s.find(word, wordPos);
if (wordPos != string::npos) {
++count;
// start next search after this word
wordPos += word.length();
}
}

return count;
}
}





0
Sign in to follow this  
Followers 0


2 Comments


why not? I thought they may be useful to someone. They are certainly useful to me when parsing text files such as Wavefront object models, ini files etc.
0

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now