1 // Add (std::string) like methods to Nasal strings
3 // Copyright (C) 2013 Thomas Geymayer <tomgey@gmail.com>
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.
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.
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.
23 #include "NasalString.hxx"
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>
31 * Compare (sub)string with other string
34 * compare(pos, len, s)
36 static naRef f_compare(naContext c, naRef me, int argc, naRef* args)
38 nasal::CallContext ctx(c, argc, args);
39 nasal::String str = nasal::from_nasal<nasal::String>(c, me),
40 rhs = ctx.requireArg<nasal::String>(argc > 1 ? 2 : 0);
41 size_t pos = argc > 1 ? ctx.requireArg<int>(1) : 0;
42 size_t len = argc > 1 ? ctx.requireArg<int>(2) : 0;
45 len = nasal::String::npos;
47 return naNum( str.compare(pos, len, rhs) );
51 * Check whether string starts with other string
53 static naRef f_starts_with(naContext c, naRef me, int argc, naRef* args)
55 nasal::CallContext ctx(c, argc, args);
56 nasal::String str = nasal::from_nasal<nasal::String>(c, me),
57 rhs = ctx.requireArg<nasal::String>(0);
59 return naNum( str.starts_with(rhs) );
63 * Helper to convert size_t position/npos to Nasal conventions (-1 == npos)
65 naRef pos_to_nasal(size_t pos)
67 if( pos == nasal::String::npos )
74 * Find first occurrence of single character
78 static naRef f_find(naContext c, naRef me, int argc, naRef* args)
80 nasal::CallContext ctx(c, argc, args);
81 nasal::String str = nasal::from_nasal<nasal::String>(c, me),
82 find = ctx.requireArg<nasal::String>(0);
83 size_t pos = ctx.getArg<int>(1, 0);
85 if( find.size() != 1 )
86 naRuntimeError(c, "string::find: single character expected");
88 return pos_to_nasal( str.find(*find.c_str(), pos) );
92 * Find first character of a string occurring in this string
94 * find_first_of(search, pos = 0)
96 static naRef f_find_first_of(naContext c, naRef me, int argc, naRef* args)
98 nasal::CallContext ctx(c, argc, args);
99 nasal::String str = nasal::from_nasal<nasal::String>(c, me),
100 find = ctx.requireArg<nasal::String>(0);
101 size_t pos = ctx.getArg<int>(1, 0);
103 return pos_to_nasal( str.find_first_of(find, pos) );
107 * Find first character of this string not occurring in the other string
109 * find_first_not_of(search, pos = 0)
111 static naRef f_find_first_not_of(naContext c, naRef me, int argc, naRef* args)
113 nasal::CallContext ctx(c, argc, args);
114 nasal::String str = nasal::from_nasal<nasal::String>(c, me),
115 find = ctx.requireArg<nasal::String>(0);
116 size_t pos = ctx.getArg<int>(1, 0);
118 return pos_to_nasal( str.find_first_not_of(find, pos) );
121 //------------------------------------------------------------------------------
122 naRef initNasalString(naRef globals, naRef string, naContext c, naRef gcSave)
124 nasal::Hash string_module(string, c);
126 string_module.set("compare", f_compare);
127 string_module.set("starts_with", f_starts_with);
128 string_module.set("find", f_find);
129 string_module.set("find_first_of", f_find_first_of);
130 string_module.set("find_first_not_of", f_find_first_not_of);