wp/wp-includes/pomo/plural-forms.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
   101 		$stack  = array();
   101 		$stack  = array();
   102 		while ( $pos < $len ) {
   102 		while ( $pos < $len ) {
   103 			$next = substr( $str, $pos, 1 );
   103 			$next = substr( $str, $pos, 1 );
   104 
   104 
   105 			switch ( $next ) {
   105 			switch ( $next ) {
   106 				// Ignore whitespace
   106 				// Ignore whitespace.
   107 				case ' ':
   107 				case ' ':
   108 				case "\t":
   108 				case "\t":
   109 					$pos++;
   109 					$pos++;
   110 					break;
   110 					break;
   111 
   111 
   112 				// Variable (n)
   112 				// Variable (n).
   113 				case 'n':
   113 				case 'n':
   114 					$output[] = array( 'var' );
   114 					$output[] = array( 'var' );
   115 					$pos++;
   115 					$pos++;
   116 					break;
   116 					break;
   117 
   117 
   118 				// Parentheses
   118 				// Parentheses.
   119 				case '(':
   119 				case '(':
   120 					$stack[] = $next;
   120 					$stack[] = $next;
   121 					$pos++;
   121 					$pos++;
   122 					break;
   122 					break;
   123 
   123 
   124 				case ')':
   124 				case ')':
   125 					$found = false;
   125 					$found = false;
   126 					while ( ! empty( $stack ) ) {
   126 					while ( ! empty( $stack ) ) {
   127 						$o2 = $stack[ count( $stack ) - 1 ];
   127 						$o2 = $stack[ count( $stack ) - 1 ];
   128 						if ( $o2 !== '(' ) {
   128 						if ( '(' !== $o2 ) {
   129 							$output[] = array( 'op', array_pop( $stack ) );
   129 							$output[] = array( 'op', array_pop( $stack ) );
   130 							continue;
   130 							continue;
   131 						}
   131 						}
   132 
   132 
   133 						// Discard open paren.
   133 						// Discard open paren.
   141 					}
   141 					}
   142 
   142 
   143 					$pos++;
   143 					$pos++;
   144 					break;
   144 					break;
   145 
   145 
   146 				// Operators
   146 				// Operators.
   147 				case '|':
   147 				case '|':
   148 				case '&':
   148 				case '&':
   149 				case '>':
   149 				case '>':
   150 				case '<':
   150 				case '<':
   151 				case '!':
   151 				case '!':
   159 					}
   159 					}
   160 
   160 
   161 					while ( ! empty( $stack ) ) {
   161 					while ( ! empty( $stack ) ) {
   162 						$o2 = $stack[ count( $stack ) - 1 ];
   162 						$o2 = $stack[ count( $stack ) - 1 ];
   163 
   163 
   164 						// Ternary is right-associative in C
   164 						// Ternary is right-associative in C.
   165 						if ( $operator === '?:' || $operator === '?' ) {
   165 						if ( '?:' === $operator || '?' === $operator ) {
   166 							if ( self::$op_precedence[ $operator ] >= self::$op_precedence[ $o2 ] ) {
   166 							if ( self::$op_precedence[ $operator ] >= self::$op_precedence[ $o2 ] ) {
   167 								break;
   167 								break;
   168 							}
   168 							}
   169 						} elseif ( self::$op_precedence[ $operator ] > self::$op_precedence[ $o2 ] ) {
   169 						} elseif ( self::$op_precedence[ $operator ] > self::$op_precedence[ $o2 ] ) {
   170 							break;
   170 							break;
   175 					$stack[] = $operator;
   175 					$stack[] = $operator;
   176 
   176 
   177 					$pos += $end_operator;
   177 					$pos += $end_operator;
   178 					break;
   178 					break;
   179 
   179 
   180 				// Ternary "else"
   180 				// Ternary "else".
   181 				case ':':
   181 				case ':':
   182 					$found = false;
   182 					$found = false;
   183 					$s_pos = count( $stack ) - 1;
   183 					$s_pos = count( $stack ) - 1;
   184 					while ( $s_pos >= 0 ) {
   184 					while ( $s_pos >= 0 ) {
   185 						$o2 = $stack[ $s_pos ];
   185 						$o2 = $stack[ $s_pos ];
   186 						if ( $o2 !== '?' ) {
   186 						if ( '?' !== $o2 ) {
   187 							$output[] = array( 'op', array_pop( $stack ) );
   187 							$output[] = array( 'op', array_pop( $stack ) );
   188 							$s_pos--;
   188 							$s_pos--;
   189 							continue;
   189 							continue;
   190 						}
   190 						}
   191 
   191 
   199 						throw new Exception( 'Missing starting "?" ternary operator' );
   199 						throw new Exception( 'Missing starting "?" ternary operator' );
   200 					}
   200 					}
   201 					$pos++;
   201 					$pos++;
   202 					break;
   202 					break;
   203 
   203 
   204 				// Default - number or invalid
   204 				// Default - number or invalid.
   205 				default:
   205 				default:
   206 					if ( $next >= '0' && $next <= '9' ) {
   206 					if ( $next >= '0' && $next <= '9' ) {
   207 						$span     = strspn( $str, self::NUM_CHARS, $pos );
   207 						$span     = strspn( $str, self::NUM_CHARS, $pos );
   208 						$output[] = array( 'value', intval( substr( $str, $pos, $span ) ) );
   208 						$output[] = array( 'value', intval( substr( $str, $pos, $span ) ) );
   209 						$pos     += $span;
   209 						$pos     += $span;
   214 			}
   214 			}
   215 		}
   215 		}
   216 
   216 
   217 		while ( ! empty( $stack ) ) {
   217 		while ( ! empty( $stack ) ) {
   218 			$o2 = array_pop( $stack );
   218 			$o2 = array_pop( $stack );
   219 			if ( $o2 === '(' || $o2 === ')' ) {
   219 			if ( '(' === $o2 || ')' === $o2 ) {
   220 				throw new Exception( 'Mismatched parentheses' );
   220 				throw new Exception( 'Mismatched parentheses' );
   221 			}
   221 			}
   222 
   222 
   223 			$output[] = array( 'op', $o2 );
   223 			$output[] = array( 'op', $o2 );
   224 		}
   224 		}
   238 	 */
   238 	 */
   239 	public function get( $num ) {
   239 	public function get( $num ) {
   240 		if ( isset( $this->cache[ $num ] ) ) {
   240 		if ( isset( $this->cache[ $num ] ) ) {
   241 			return $this->cache[ $num ];
   241 			return $this->cache[ $num ];
   242 		}
   242 		}
   243 		return $this->cache[ $num ] = $this->execute( $num );
   243 		$this->cache[ $num ] = $this->execute( $num );
       
   244 		return $this->cache[ $num ];
   244 	}
   245 	}
   245 
   246 
   246 	/**
   247 	/**
   247 	 * Execute the plural form function.
   248 	 * Execute the plural form function.
   248 	 *
   249 	 *
   256 		$i     = 0;
   257 		$i     = 0;
   257 		$total = count( $this->tokens );
   258 		$total = count( $this->tokens );
   258 		while ( $i < $total ) {
   259 		while ( $i < $total ) {
   259 			$next = $this->tokens[ $i ];
   260 			$next = $this->tokens[ $i ];
   260 			$i++;
   261 			$i++;
   261 			if ( $next[0] === 'var' ) {
   262 			if ( 'var' === $next[0] ) {
   262 				$stack[] = $n;
   263 				$stack[] = $n;
   263 				continue;
   264 				continue;
   264 			} elseif ( $next[0] === 'value' ) {
   265 			} elseif ( 'value' === $next[0] ) {
   265 				$stack[] = $next[1];
   266 				$stack[] = $next[1];
   266 				continue;
   267 				continue;
   267 			}
   268 			}
   268 
   269 
   269 			// Only operators left.
   270 			// Only operators left.