root / trunk / docscripts / inc / Text.php

Revision 6171, 4.9 kB (checked in by pottedmeat, 2 years ago)

Enable extend/mixin, parameters and optional comments in the pretty_xml, and comment break on a blank line

Line 
1<?php
2
3class Text
4{
5  /**
6   * Blanks out a portion of a string with whitespace
7   *
8   * @param $to_blank Portion of the string to be removed
9   * @param $string Overall string to remove it from
10   */
11  public static function blankOut($to_blank, $string)
12  {
13    $length = strlen($to_blank);
14    if (!$length) {
15      return $string;
16    }
17
18    $blanks = array_fill(0, $length, ' ');
19    return preg_replace('%' . preg_quote($to_blank, '%') . '%', implode($blanks), $string, 1);
20  }
21 
22  public static function getNextPosition($array, $line_position_pair)
23  {
24    list($line_number, $position) = $line_position_pair;
25    ++$position;
26    if ($position >= strlen($array[$line_number])) {
27      ++$line_number;
28      $position = 0;
29      while (!strlen($array[$line_number])) {
30        ++$line_number;
31      }
32    }
33    return array($line_number, $position);
34  }
35 
36  public static function blankOutAt($to_blank, $start, $end = -1)
37  {
38    if($end == -1) {
39      $end = strlen($to_blank) - 1;
40    }
41    $length = $end - $start + 1;
42    if (!$length) {
43      return $to_blank;
44    }
45    if ($length < 0) {
46      print 'hi';
47    }
48    $blanks = array_fill(0, $length, ' ');
49    return substr($to_blank, 0, $start) . implode($blanks) .  substr($to_blank, $end + 1);
50  }
51 
52  public static function trim($string)
53  {
54    return trim(preg_replace('%(^\s*/\*.*\*/\s*?|\s*?/\*.*\*/\s*$|^\s*//.*\n\s*?|\s*?//.*$)%U', '', $string));
55  }
56 
57  public static function chop($array, $start_line, $start_position, $end_line = false, $end_position = false, $exclusive = false)
58  {
59    if (!is_numeric($end_line)) {
60      $end_line = end(array_keys($array));
61    }
62    if (!is_numeric($end_position)) {
63      $end_position = strlen($array[$end_line]) - 1;
64      if($end_position < 0){
65        $end_position = 0;
66      }
67    }
68   
69    $lines = array_slice($array, $start_line, $end_line - $start_line + 1, true);
70    if ($start_position > 0) {
71      $lines[$start_line] = Text::blankOutAt($lines[$start_line], 0, $start_position - 1);
72    }
73    $lines[$end_line] = Text::blankOutAt($lines[$end_line], $end_position + 1, strlen($lines[$end_line]));
74    if ($exclusive) {
75      if ($lines[$start_line]{$start_position}) {
76        $lines[$start_line]{$start_position} = ' ';
77      }
78      if ($lines[$end_line]{$end_position}) {
79        $lines[$end_line]{$end_position} = ' ';
80      }
81    }
82   
83    return $lines;
84  }
85 
86  /**
87   * Always starts at the beginning. If you want a character to be ignored, it shouldn't be passed (see chop and blankOutAt)
88   */
89  public static function findTermination($source_array, $termination_characters, $enclosing_characters = '')
90  {
91    $characters = array();
92    $terminators = array();
93    foreach(self::toArray($termination_characters) as $character) {
94      $terminators[$character] = true;
95    }
96    foreach (self::toArray($enclosing_characters) as $index => $character) {
97      $characters[$character] = ($index % 2) ? -1 : 1;
98    }
99    $all_characters = array_merge(array_keys($terminators), array_keys($characters));
100   
101    $balance = 0;
102   
103    foreach ($source_array as $line_number => $line) {
104      $line = self::toArray($line);
105      foreach (array_intersect($line, $all_characters) as $position => $character) {
106        if (!$balance && $terminators[$character]) {
107          return array($line_number, $position);
108        }
109        $balance += $characters[$character];
110      }
111    }
112   
113    return array($line_number, $position);
114  }
115 
116  public static function toArray($string)
117  {
118    return array_slice(preg_split('%%', $string), 1, -1);
119  }
120 
121  public static function findComments($line, $started = false)
122  {
123    if (empty($line) && !$started) {
124      return array(false, false, false, false, false);
125    }
126   
127    $first = array();
128    $middle = array();
129    $last = array();
130    $data = false;
131    $multiline = false;
132   
133    if ($started) {
134      if (($pos = strpos($line, '*/')) !== false) {
135        $first[] = trim(substr($line, 0, $pos));
136        $line = substr($line, $pos + 2);
137      }
138      else {
139        $multiline = true;
140      }
141    }
142
143    $single_line = false;
144    if (!$multiline) {
145      $parts = preg_split('%(\s*(?://|/\*|\*/)\s*)%', $line, -1, PREG_SPLIT_DELIM_CAPTURE);
146      foreach ($parts as $part) {
147        if (!($part = trim($part))) continue;
148        if ($multiline && $part == '*/') {
149          $multiline = false;
150        }
151        elseif ($single_line || $multiline) {
152          if (!$data) {
153            $first[] = $part;
154          }
155          else {
156            $last[] = $part;
157          }
158        }
159        elseif ($part == '//') {
160          $single_line = true;
161        }
162        elseif ($part == '/*') {
163          $multiline = true;
164        }
165        else {
166          $data = true;
167          $middle = array_merge($middle, $last);
168          $last = array();
169        }
170      }
171    }
172
173    return array(implode(' ', $first), implode(' ', $middle), implode(' ', $last), $data, $multiline);
174  }
175}
176
177?>
Note: See TracBrowser for help on using the browser.