cvs: pear /File_IMC/File/IMC Parse.php

From: Date: Sun, 16 Nov 2003 14:42:16 +0000
Subject: cvs: pear /File_IMC/File/IMC Parse.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-14304@lists.php.net to get a copy of this message
pmjones Sun Nov 16 09:42:16 2003 EDT Modified files: /pear/File_IMC/File/IMC Parse.php Log: All changes are to make splitByColon work as expected. Wrote step-by-stemp comments for Marshall's new char-by-char splitByDelim() method (which is quite nice). Added an initial $inQuotes value in splitByDelim(). Removed $convertSingle paramter from splitByDelim() (we have agreed that all results are to be arrays, not strings). In its place, added $recurse parameter to splitByDelim(), this tells the method whether or not to recursively parse past the first occurrence of the delimiter. Added $recurse params and default vals to splitBySemi() (true), splitByComma (true), and splitByColon (false). Tested it with sample vCard parse, appears to work as expected. Index: pear/File_IMC/File/IMC/Parse.php diff -u pear/File_IMC/File/IMC/Parse.php:1.7 pear/File_IMC/File/IMC/Parse.php:1.8 --- pear/File_IMC/File/IMC/Parse.php:1.7 Thu Nov 13 20:55:38 2003 +++ pear/File_IMC/File/IMC/Parse.php Sun Nov 16 09:42:16 2003 @@ -16,7 +16,7 @@ // | Authors: Paul M. Jones <pjones@ciaweb.net> | // +----------------------------------------------------------------------+ // -// $Id: Parse.php,v 1.7 2003/11/14 01:55:38 mroch Exp $ +// $Id: Parse.php,v 1.8 2003/11/16 14:42:16 pmjones Exp $ /** * @@ -153,10 +153,10 @@ * @param string $text The string to split into an array. * * @param string $delim Character to split string at. - * - * @param bool $convertSingle If splitting the string results in a - * single array element, return a string instead of a one-element - * array. + * + * @param bool $recurse If true, recursively parse the entire text + * for all occurrences of the delimiter; if false, only parse for + * the first occurrence. Defaults to true. * * @return string|array An array of values, or a single string. * @@ -164,45 +164,83 @@ * */ - function splitByDelim($text, $delim) + function splitByDelim($text, $delim, $recurse = true) { + // where in the string is the delimiter? $pos = false; + + // was the previously-read character a backslash? + // (used for tracking escaped characters) $prevIsBackslash = false; + + // are we currently inside a quoted passage? + $inQuotes == false; + + // the length of the text to be parsed $len = strlen($text); + + // go through the text character by character, find the + // first occurrence of the delimiter, save it, and + // recursively parse the rest of the text for ($i = 0; $i < $len; $i++) { - if ($text{$i} == "\"" && $prevIsBackslash == false) { + + // if the current char is a double-quote, and the + // previous char was _not_ an escaping backslash, + // then note that we are now inside a quoted passage. + if ($text{$i} == '"' && $prevIsBackslash == false) { ($inQuotes == true) ? $inQuotes = false : $inQuotes = true; } - if ($text{$i} == $delim && $inQuotes == false && $prevIsBackslash == false) { + // if the current char is the delimiter, and we are _not_ + // inside quotes, and the delimiter has not been backslash- + // escaped, then note the position of the delimiter and + // break out of the loop. + if ($text{$i} == $delim && + $inQuotes == false && + $prevIsBackslash == false) { + $pos = $i; break; + } - + + // we have not found quotes, or the delimiter. + // is the current char an escaping backslash? if ($text{$i} == "\\") { $prevIsBackslash = true; } else { $prevIsBackslash = false; } } - + + // have we found the delimiter in the text? if ($pos === false) { + + // we have not found the delimiter anywhere in the + // text. return the text as it is. return array($text); - } - - $left = trim(substr($text, 0, $pos)); - $right = trim(substr($text, $pos+1, strlen($text))); - - $result[] = $left; - $tmp = array_merge($result, $this->splitByDelim($right, $delim)); - - // if there is only one array-element and $convertSingle is - // true, then return only the value of that one array element - // (instead of returning the array). - if ($convertSingle && count($tmp) == 1) { - return $tmp[0]; + } else { - return $tmp; + + // find the portions of the text to the left and the + // right of the delimiter + $left = trim(substr($text, 0, $pos)); + $right = trim(substr($text, $pos+1, strlen($text))); + + // should we recursively parse the rest of the text? + if ($recurse) { + + // parse the right portion for the same delimiter, and + // merge the results with the left-portion. + return array_merge( + array($left), + $this->splitByDelim($right, $delim, $recurse) + ); + + } else { + // no recursion + return array($left, $right); + } } } @@ -215,6 +253,10 @@ * @param bool $convertSingle If splitting the string results in a * single array element, return a string instead of a one-element * array. + * + * @param bool $recurse If true, recursively parse the entire text + * for all occurrences of the delimiter; if false, only parse for + * the first occurrence. Defaults to true. * * @return string|array An array of values, or a single string. * @@ -224,9 +266,9 @@ * */ - function splitBySemi($text, $convertSingle = false) + function splitBySemi($text, $recurse = true) { - return $this->splitByDelim($text, ";", $convertSingle); + return $this->splitByDelim($text, ";", $recurse); } @@ -235,10 +277,10 @@ * Splits a string into an array at commas. * * @param string $text The string to split into an array. - * - * @param bool $convertSingle If splitting the string results in a - * single array element, return a string instead of a one-element - * array. + * + * @param bool $recurse If true, recursively parse the entire text + * for all occurrences of the delimiter; if false, only parse for + * the first occurrence. Defaults to true. * * @return string|array An array of values, or a single string. * @@ -248,9 +290,9 @@ * */ - function splitByComma($text, $convertSingle = false) + function splitByComma($text, $recurse = true) { - return $this->splitByDelim($text, ",", $convertSingle); + return $this->splitByDelim($text, ",", $recurse); } @@ -263,18 +305,22 @@ * * @param string $text The string to split into an array. * + * @param bool $recurse If true, recursively parse the entire text + * for all occurrences of the delimiter; if false, only parse for + * the first occurrence. Defaults to false (this is different from + * splitByCommon and splitBySemi). + * * @return array The first element contains types and parameters - * (before the colon). - * The second element contains the line's value - * (after the colon). + * (before the colon). The second element contains the line's value + * (after the colon). * * @access public * */ - function splitByColon($text) + function splitByColon($text, $recurse = false) { - return $this->splitByDelim($text, ":"); + return $this->splitByDelim($text, ":", $recurse); } @@ -427,9 +473,9 @@ /** * - * Takes a line and extracts the Group for the line (a group is - * identified as a prefix-with-dot to the Type-Definition; e.g., - * Group.ADR or Group.ORG). + * Takes a line and extracts the Group for the line (a group is + * identified as a prefix-with-dot to the Type-Definition; e.g., + * Group.ADR or Group.ORG). * * @param array Array containing left side (before colon) split by * semi-colon from a line.

« previous php.pear.cvs (#14304) next »