FlightGear next
NasalString.cxx
Go to the documentation of this file.
1// Add (std::string) like methods to Nasal strings
2//
3// Copyright (C) 2013 Thomas Geymayer <tomgey@gmail.com>
4//
5// This program is free software; you can redistribute it and/or
6// modify it under the terms of the GNU General Public License as
7// published by the Free Software Foundation; either version 2 of the
8// License, or (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful, but
11// WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13// General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program; if not, write to the Free Software
17// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19#ifdef HAVE_CONFIG_H
20# include "config.h"
21#endif
22
23#include "NasalString.hxx"
24
25#include <simgear/nasal/cppbind/from_nasal.hxx>
26#include <simgear/nasal/cppbind/Ghost.hxx>
27#include <simgear/nasal/cppbind/NasalHash.hxx>
28#include <simgear/nasal/cppbind/NasalString.hxx>
29
36static naRef f_compare(const nasal::CallContext& ctx)
37{
38 nasal::String str = ctx.from_nasal<nasal::String>(ctx.me),
39 rhs = ctx.requireArg<nasal::String>(ctx.argc > 1 ? 2 : 0);
40 size_t pos = ctx.argc > 1 ? ctx.requireArg<int>(1) : 0;
41 size_t len = ctx.argc > 1 ? ctx.requireArg<int>(2) : 0;
42
43 if( len == 0 )
44 len = nasal::String::npos;
45
46 return naNum( str.compare(pos, len, rhs) );
47}
48
52static naRef f_starts_with(const nasal::CallContext& ctx)
53{
54 nasal::String str = ctx.from_nasal<nasal::String>(ctx.me),
55 rhs = ctx.requireArg<nasal::String>(0);
56
57 return naNum( str.starts_with(rhs) );
58}
59
63static naRef f_ends_with(const nasal::CallContext& ctx)
64{
65 nasal::String str = ctx.from_nasal<nasal::String>(ctx.me),
66 rhs = ctx.requireArg<nasal::String>(0);
67
68 return naNum( str.ends_with(rhs) );
69}
70
74naRef pos_to_nasal(size_t pos)
75{
76 if( pos == nasal::String::npos )
77 return naNum(-1);
78 else
79 return naNum(pos);
80}
81
87static naRef f_find(const nasal::CallContext& ctx)
88{
89 nasal::String str = ctx.from_nasal<nasal::String>(ctx.me),
90 find = ctx.requireArg<nasal::String>(0);
91 size_t pos = ctx.getArg<int>(1, 0);
92
93 if( find.size() != 1 )
94 ctx.runtimeError("string::find: single character expected");
95
96 return pos_to_nasal( str.find(*find.c_str(), pos) );
97}
98
104static naRef f_find_first_of(const nasal::CallContext& ctx)
105{
106 nasal::String str = ctx.from_nasal<nasal::String>(ctx.me),
107 find = ctx.requireArg<nasal::String>(0);
108 size_t pos = ctx.getArg<int>(1, 0);
109
110 return pos_to_nasal( str.find_first_of(find, pos) );
111}
112
118static naRef f_find_first_not_of(const nasal::CallContext& ctx)
119{
120 nasal::String str = ctx.from_nasal<nasal::String>(ctx.me),
121 find = ctx.requireArg<nasal::String>(0);
122 size_t pos = ctx.getArg<int>(1, 0);
123
124 return pos_to_nasal( str.find_first_not_of(find, pos) );
125}
126
127//------------------------------------------------------------------------------
128naRef initNasalString(naRef globals, naRef string, naContext c)
129{
130 nasal::Hash string_module(string, c);
131
132 string_module.set("compare", f_compare);
133 string_module.set("starts_with", f_starts_with);
134 string_module.set("ends_with", f_ends_with);
135 string_module.set("find", f_find);
136 string_module.set("find_first_of", f_find_first_of);
137 string_module.set("find_first_not_of", f_find_first_not_of);
138
139 return naNil();
140}
static naRef f_find(const nasal::CallContext &ctx)
Find first occurrence of single character.
naRef pos_to_nasal(size_t pos)
Helper to convert size_t position/npos to Nasal conventions (-1 == npos)
static naRef f_find_first_of(const nasal::CallContext &ctx)
Find first character of a string occurring in this string.
naRef initNasalString(naRef globals, naRef string, naContext c)
static naRef f_starts_with(const nasal::CallContext &ctx)
Check whether string starts with other string.
static naRef f_ends_with(const nasal::CallContext &ctx)
Check whether string ends with other string.
static naRef f_find_first_not_of(const nasal::CallContext &ctx)
Find first character of this string not occurring in the other string.
static naRef f_compare(const nasal::CallContext &ctx)
Compare (sub)string with other string.
FGGlobals * globals
Definition globals.cxx:142