Skip to content

Commit 7b96aab

Browse files
committed
string index function
1 parent c004b57 commit 7b96aab

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

context.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ namespace Sass {
363363
register_function(ctx, quote_sig, sass_quote, env);
364364
register_function(ctx, str_length_sig, str_length, env);
365365
register_function(ctx, str_insert_sig, str_insert, env);
366+
register_function(ctx, str_index_sig, str_index, env);
366367
// Number Functions
367368
register_function(ctx, percentage_sig, percentage, env);
368369
register_function(ctx, round_sig, round, env);

functions.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,25 @@ namespace Sass {
752752

753753
}
754754

755+
Signature str_index_sig = "str-index($string, $substring)";
756+
BUILT_IN(str_index)
757+
{
758+
String_Constant* s = ARG("$string", String_Constant);
759+
String_Constant* t = ARG("$substring", String_Constant);
760+
string str = s->value();
761+
str = unquote(str);
762+
string substr = t->value();
763+
substr = unquote(substr);
764+
765+
size_t c_index = str.find(substr);
766+
if(c_index == string::npos) {
767+
return new (ctx.mem) Null(path, position);
768+
}
769+
size_t index = UTF_8::code_point_count(str, 0, c_index + 1);
770+
771+
return new (ctx.mem) Number(path, position, index);
772+
}
773+
755774
///////////////////
756775
// NUMBER FUNCTIONS
757776
///////////////////

functions.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ namespace Sass {
6666
extern Signature quote_sig;
6767
extern Signature str_length_sig;
6868
extern Signature str_insert_sig;
69+
extern Signature str_index_sig;
6970
extern Signature percentage_sig;
7071
extern Signature round_sig;
7172
extern Signature ceil_sig;
@@ -119,6 +120,7 @@ namespace Sass {
119120
BUILT_IN(sass_quote);
120121
BUILT_IN(str_length);
121122
BUILT_IN(str_insert);
123+
BUILT_IN(str_index);
122124
BUILT_IN(percentage);
123125
BUILT_IN(round);
124126
BUILT_IN(ceil);

0 commit comments

Comments
 (0)