@@ -709,6 +709,49 @@ namespace Sass {
709709 return new (ctx.mem ) Number (path, position, len);
710710 }
711711
712+ Signature str_insert_sig = " str-insert($string, $insert, $index)" ;
713+ BUILT_IN (str_insert)
714+ {
715+ String_Constant* s = ARG (" $string" , String_Constant);
716+ string str = s->value ();
717+ char quotemark = s->quote_mark ();
718+ str = unquote (str);
719+ String_Constant* i = ARG (" $insert" , String_Constant);
720+ string ins = i->value ();
721+ ins = unquote (ins);
722+ Number* ind = ARG (" $index" , Number);
723+ double index = ind->value ();
724+ size_t len = UTF_8::code_point_count (str, 0 , str.size ());
725+
726+ if (index > 0 && index <= len) {
727+ // positive and within string length
728+ str.insert (UTF_8::code_point_offset_to_byte_offset (str, index-1 ), ins);
729+ }
730+ else if (index > len) {
731+ // positive and past string length
732+ str += ins;
733+ }
734+ else if (index == 0 ) {
735+ str = ins + str;
736+ }
737+ else if (std::abs (index) <= len) {
738+ // negative and within string length
739+ index += len + 1 ;
740+ str.insert (UTF_8::code_point_offset_to_byte_offset (str, index), ins);
741+ }
742+ else {
743+ // negative and past string length
744+ str = ins + str;
745+ }
746+
747+ if (quotemark) {
748+ str = quote (str, quotemark);
749+ }
750+
751+ return new (ctx.mem ) String_Constant (path, position, str);
752+
753+ }
754+
712755 // /////////////////
713756 // NUMBER FUNCTIONS
714757 // /////////////////
0 commit comments