1ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved. 2c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Use of this source code is governed by a BSD-style license that can be 3c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// found in the LICENSE file. 4c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 5c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// This file defines utility functions for escaping strings. 6c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 7c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#ifndef BASE_JSON_STRING_ESCAPE_H_ 8c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#define BASE_JSON_STRING_ESCAPE_H_ 93345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#pragma once 10c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 11c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <string> 12c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 13ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/base_api.h" 14c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/string16.h" 15c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 16c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottnamespace base { 17c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 18c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Escape |str| appropriately for a JSON string literal, _appending_ the 19c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// result to |dst|. This will create unicode escape sequences (\uXXXX). 20c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// If |put_in_quotes| is true, the result will be surrounded in double quotes. 21c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// The outputted literal, when interpreted by the browser, should result in a 22c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// javascript string that is identical and the same length as the input |str|. 23ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API void JsonDoubleQuote(const std::string& str, 24ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen bool put_in_quotes, 25ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen std::string* dst); 26c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 27c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Same as above, but always returns the result double quoted. 28ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API std::string GetDoubleQuotedJson(const std::string& str); 29c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 30ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API void JsonDoubleQuote(const string16& str, 31ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen bool put_in_quotes, 32ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen std::string* dst); 33c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 34c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Same as above, but always returns the result double quoted. 35ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API std::string GetDoubleQuotedJson(const string16& str); 36c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 37c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} // namespace base 38c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 39c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif // BASE_JSON_STRING_ESCAPE_H_ 40