Skip to content

Commit cd3ee1c

Browse files
author
Aaron Leung
committed
Merge pull request #356 from wonja/str_len_func
adding string length built-in function
2 parents 983aa9e + 1e98e20 commit cd3ee1c

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

context.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ namespace Sass {
361361
// String Functions
362362
register_function(ctx, unquote_sig, sass_unquote, env);
363363
register_function(ctx, quote_sig, sass_quote, env);
364+
register_function(ctx, str_length_sig, str_length, env);
364365
// Number Functions
365366
register_function(ctx, percentage_sig, percentage, env);
366367
register_function(ctx, round_sig, round, env);

functions.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,42 @@ namespace Sass {
689689
return result;
690690
}
691691

692+
693+
Signature str_length_sig = "str-length($string)";
694+
BUILT_IN(str_length)
695+
{
696+
String_Constant* s = ARG("$string", String_Constant);
697+
string str = s->value();
698+
size_t len = 0;
699+
size_t length_of_s = str.size();
700+
size_t i = 0;
701+
702+
if (s->is_quoted()) {
703+
++i;
704+
--length_of_s;
705+
}
706+
707+
while (i < length_of_s) {
708+
unsigned char c = static_cast<unsigned char>(str[i]);
709+
if (c < 128) {
710+
// it's a single-byte character
711+
++len;
712+
++i;
713+
}
714+
// it's a multi bit sequence and presumably it's a leading bit
715+
else {
716+
++i; // go to the next byte
717+
// see if it's still part of the sequence
718+
while ((i < length_of_s) && ((static_cast<unsigned char>(str[i]) & 0b11000000) == 0b10000000)) {
719+
++i;
720+
}
721+
// when it's not [aka a new leading bit], increment and move on
722+
++len;
723+
}
724+
}
725+
return new (ctx.mem) Number(path, position, len);
726+
}
727+
692728
///////////////////
693729
// NUMBER FUNCTIONS
694730
///////////////////

functions.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ namespace Sass {
6464
extern Signature ie_hex_str_sig;
6565
extern Signature unquote_sig;
6666
extern Signature quote_sig;
67+
extern Signature str_length_sig;
6768
extern Signature percentage_sig;
6869
extern Signature round_sig;
6970
extern Signature ceil_sig;
@@ -115,6 +116,7 @@ namespace Sass {
115116
BUILT_IN(ie_hex_str);
116117
BUILT_IN(sass_unquote);
117118
BUILT_IN(sass_quote);
119+
BUILT_IN(str_length);
118120
BUILT_IN(percentage);
119121
BUILT_IN(round);
120122
BUILT_IN(ceil);

0 commit comments

Comments
 (0)