PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/xhp/code_rope.cpp

http://github.com/facebook/xhp
C++ | 113 lines | 82 code | 14 blank | 17 comment | 17 complexity | aeefe6e16c884400c0ae5bdd9fb49ea1 MD5 | raw file
Possible License(s): MIT, MPL-2.0-no-copyleft-exception
  1. /*
  2. +----------------------------------------------------------------------+
  3. | XHP |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2009 - 2010 Facebook, Inc. (http://www.facebook.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE.PHP, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include "code_rope.hpp"
  17. using namespace std;
  18. code_rope::code_rope(const _rope_t str, const size_t no /* = 0 */, const size_t lf /* = 0 */) : str(str), lf(lf), no(no) {}
  19. code_rope::code_rope(const code_rope& str, const size_t no /* = 0 */, const size_t lf /* = 0 */) : str(str.str), lf(lf), no(no) {
  20. if (str.lf || str.no) {
  21. if (!no && !lf) {
  22. this->lf = str.lf;
  23. this->no = str.no;
  24. } else {
  25. throw new std::exception();
  26. }
  27. } else {
  28. this->no = no;
  29. this->lf = lf;
  30. }
  31. }
  32. const char* code_rope::c_str() const {
  33. if (0 && this->no > 1) {
  34. return NULL;
  35. // lolololololol
  36. // this code is clowntown -- returns dealloced memory
  37. _rope_t whitespace(this->no - 1, '\n');
  38. whitespace += this->str;
  39. return whitespace.c_str();
  40. } else {
  41. return this->str.c_str();
  42. }
  43. }
  44. void code_rope::prepend(const char* str) {
  45. this->str = _rope_t(str) + this->str;
  46. }
  47. const char code_rope::back() const {
  48. return this->str.empty() ? 0 : this->str.back();
  49. }
  50. void code_rope::pop_back() {
  51. this->str.pop_back();
  52. }
  53. void code_rope::strip_lines() {
  54. lf = no = 0;
  55. }
  56. size_t code_rope::lineno() const {
  57. return no;
  58. }
  59. code_rope code_rope::operator+(const code_rope& right) const {
  60. size_t diff;
  61. size_t no, lf;
  62. _rope_t glue;
  63. if (this->no && right.no) {
  64. no = this->no;
  65. if (right.no > this->no + this->lf) {
  66. diff = right.no - this->no - this->lf;
  67. lf = this->lf + right.lf + diff;
  68. glue = _rope_t(diff, '\n');
  69. } else {
  70. no = this->no;
  71. lf = this->lf + right.lf;
  72. }
  73. } else if (right.no) {
  74. no = right.no;
  75. lf = this->lf + right.lf;
  76. } else {
  77. no = this->no;
  78. lf = this->lf + right.lf;
  79. }
  80. code_rope res(this->str, no, lf);
  81. if (!glue.empty()) res.str += glue;
  82. res.str += right.str;
  83. return res;
  84. }
  85. code_rope code_rope::operator+(const char* right) const {
  86. code_rope res(this->str, this->no, this->lf);
  87. res.str += right;
  88. return res;
  89. }
  90. code_rope& code_rope::operator=(const char* str) {
  91. this->str = str;
  92. this->no = this->lf = 0;
  93. return *this;
  94. }
  95. code_rope operator+(const char* left, const code_rope& right) {
  96. code_rope ret(code_rope::_rope_t(left), right.no, right.lf);
  97. ret.str += right.str;
  98. return ret;
  99. }