CSS-Sass

 view release on metacpan or  search on metacpan

libsass/util.cpp  view on Meta::CPAN

#include<stdint.h>
#include "ast.hpp"
#include "util.hpp"
#include "prelexer.hpp"
#include "utf8/checked.h"

namespace Sass {

  #define out_of_memory() do {                    \
      fprintf(stderr, "Out of memory.\n");    \
      exit(EXIT_FAILURE);                     \
    } while (0)

  /* Sadly, sass_strdup is not portable. */
  char *sass_strdup(const char *str)
  {
    char *ret = (char*) malloc(strlen(str) + 1);
    if (ret == NULL)
      out_of_memory();
    strcpy(ret, str);
    return ret;
  }

  /* Locale unspecific atof function. */
  double sass_atof(const char *str)
  {
    char separator = *(localeconv()->decimal_point);
    if(separator != '.'){
      // The current locale specifies another
      // separator. convert the separator to the
      // one understood by the locale if needed
      const char *found = strchr(str, '.');
      if(found != NULL){
        // substitution is required. perform the substitution on a copy
        // of the string. This is slower but it is thread safe.
        char *copy = sass_strdup(str);
        *(copy + (found - str)) = separator;
        double res = atof(copy);
        free(copy);
        return res;
      }
    }

    return atof(str);
  }

  string string_eval_escapes(const string& s)
  {

    string out("");
    bool esc = false;
    for (size_t i = 0, L = s.length(); i < L; ++i) {
      if(s[i] == '\\' && esc == false) {
        esc = true;

        // escape length
        size_t len = 1;

        // parse as many sequence chars as possible
        // ToDo: Check if ruby aborts after possible max
        while (i + len < L && s[i + len] && isxdigit(s[i + len])) ++ len;

        // hex string?
        if (len > 1) {

          // convert the extracted hex string to code point value
          // ToDo: Maybe we could do this without creating a substring
          uint32_t cp = strtol(s.substr (i + 1, len - 1).c_str(), nullptr, 16);

          if (cp == 0) cp = 0xFFFD;

          // assert invalid code points
          if (cp >= 1) {

            // use a very simple approach to convert via utf8 lib
            // maybe there is a more elegant way; maybe we shoud
            // convert the whole output from string to a stream!?
            // allocate memory for utf8 char and convert to utf8
            unsigned char u[5] = {0,0,0,0,0}; utf8::append(cp, u);
            for(size_t m = 0; u[m] && m < 5; m++) out.push_back(u[m]);

            // skip some more chars?
            i += len - 1; esc = false;
            if (cp == 10) out += ' ';

          }

        }

      }
      else {



( run in 0.946 second using v1.01-cache-2.11-cpan-ceb78f64989 )