1c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Copyright 2005, Google Inc.
2c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// All rights reserved.
3c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch//
4c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Redistribution and use in source and binary forms, with or without
5c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// modification, are permitted provided that the following conditions are
6c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// met:
7c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch//
8c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch//     * Redistributions of source code must retain the above copyright
9c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// notice, this list of conditions and the following disclaimer.
10c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch//     * Redistributions in binary form must reproduce the above
11c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// copyright notice, this list of conditions and the following disclaimer
12c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// in the documentation and/or other materials provided with the
13c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// distribution.
14c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch//     * Neither the name of Google Inc. nor the names of its
15c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// contributors may be used to endorse or promote products derived from
16c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// this software without specific prior written permission.
17c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch//
18c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
30c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// A sample program demonstrating using Google C++ testing framework.
31c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch//
32c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Author: wan@google.com (Zhanyong Wan)
33c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
34c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "sample2.h"
35c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
36c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include <string.h>
37c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
38c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Clones a 0-terminated C string, allocating memory using new.
39c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochconst char* MyString::CloneCString(const char* a_c_string) {
40c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  if (a_c_string == NULL) return NULL;
41c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
42c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  const size_t len = strlen(a_c_string);
43c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  char* const clone = new char[ len + 1 ];
44c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  memcpy(clone, a_c_string, len + 1);
45c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
46c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  return clone;
47c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}
48c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
49c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Sets the 0-terminated C string this MyString object
50c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// represents.
51c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochvoid MyString::Set(const char* a_c_string) {
52c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Makes sure this works when c_string == c_string_
53c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  const char* const temp = MyString::CloneCString(a_c_string);
54c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  delete[] c_string_;
55c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  c_string_ = temp;
56c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}
57