Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 98 lines (82 sloc) 2.489 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
/* vi: set expandtab shiftwidth=4 tabstop=4: */

/**
* \file
* <PRE>
* High Performance c-string to javascript-string encoder
*
* Copyright &copy; 2006, 2007 Nick Galbreath -- nickg [at] modp [dot] com
* All rights reserved.
*
* http://code.google.com/p/stringencoders/
*
* Released under bsd license. See modp_bjavascript.c for details.
* </PRE>
*/

#ifndef COM_MODP_STRINGENCODERS_BJAVASCRIPT
#define COM_MODP_STRINGENCODERS_BJAVASCRIPT

#ifdef __cplusplus
#define BEGIN_C extern "C" {
#define END_C }
#else
#define BEGIN_C
#define END_C
#endif

BEGIN_C


/**
* "javascript" encode a stirng
* This takes a c-string and does character escaping
* so it can be put into a var js_string = '...';
*
* \param[out] dest output string. Must
* \param[in] str The input string
* \param[in] len The length of the input string, excluding any
* final null byte.
*/
int modp_bjavascript_encode(char* dest, const char* str, int len);

#define modp_bjavascript_encode_len(A) (4*A + 1)

/**
* Given the exact size of output string.
*
* Can be used to allocate the right amount of memory for
* modp_burl_encode. Be sure to add 1 byte for final null.
*
* This is somewhat expensive since it examines every character
* in the input string
*
* \param[in] str The input string
* \param[in] len THe length of the input string, excluding any
* final null byte (i.e. strlen(str))
* \return the size of the output string, excluding the final
* null byte.
*/
int modp_bjavascript_encode_strlen(const char* str, int len);

END_C

#ifdef __cplusplus
#include <cstring>
#include <string>
namespace modp {

    inline std::string javascript_encode(const char* s, size_t len)
    {
        std::string x(modp_bjavascript_encode_len(len), '\0');
        int d = modp_bjavascript_encode(const_cast<char*>(x.data()), s, len);
        x.erase(d, std::string::npos);
        return x;
    }

    inline std::string javascript_encode(const char* s)
    {
        return javascript_encode(s, strlen(s));
    }

    inline std::string& javascript_encode(std::string& s)
    {
        std::string x(javascript_encode(s.data(), s.size()));
        s.swap(x);
        return s;
    }

    inline std::string javascript_encode(const std::string& s)
    {
        return javascript_encode(s.data(), s.size());
    }

} /* namespace modp */
#endif /* __cplusplus */

#endif /* modp_bjavascript */
Something went wrong with that request. Please try again.