Adding FIXME to comment
[squirrelmail.git] / functions / strings.php
index 27dfa8a1f96cecbe28cfc061369296c20ab1bf5a..242a29b967e45b34d8f390a63f15f7096b6e197b 100644 (file)
@@ -74,6 +74,35 @@ function sqMakeNewLine (&$str, $citeLevel, &$column) {
     }
 }
 
+/**
+ * Checks for spaces in strings - only used if PHP doesn't have native ctype support
+ *
+ * @author Tomas Kuliavas
+ *
+ * You might be able to rewrite the function by adding short evaluation form.
+ *
+ * possible problems:
+ *  - iso-2022-xx charsets  - hex 20 might be part of other symbol. I might
+ * be wrong. 0x20 is not used in iso-2022-jp. I haven't checked iso-2022-kr
+ * and iso-2022-cn mappings.
+ *
+ *  - no-break space ( ) - it is 8bit symbol, that depends on charset.
+ * there are at least three different charset groups that have nbsp in
+ * different places.
+ *
+ * I don't see any charset/nbsp options in php ctype either.
+ *
+ * @param string $string tested string
+ * @return bool true when only whitespace symbols are present in test string
+ */
+function sm_ctype_space($string) {
+  if ( preg_match('/^[\x09-\x0D]|^\x20/', $string) || $string=='') {
+    return true;
+  } else {
+    return false;
+  }
+}
+
 /**
  * Wraps text at $wrap characters.  While sqWordWrap takes
  * a single line of text and wraps it, this function works
@@ -87,6 +116,13 @@ function sqMakeNewLine (&$str, $citeLevel, &$column) {
  * @return string the wrapped text
  */
 function &sqBodyWrap (&$body, $wrap) {
+    //check for ctype support, and fake it if it doesn't exist
+    if (!function_exists('ctype_space')) {
+        function ctype_space ($string) {
+            return sm_ctype_space($string);
+        }
+    }
+
     // the newly wrapped text
     $outString = '';
     // current column since the last newline in the outstring
@@ -109,7 +145,9 @@ function &sqBodyWrap (&$body, $wrap) {
 
            // skip over any spaces interleaved among the cite markers
            while (($pos < $length) && ($body{$pos} == ' ')) {
+
                $pos++;
+
            }
            if ($pos >= $length) {
                break;
@@ -119,7 +157,7 @@ function &sqBodyWrap (&$body, $wrap) {
        // special case: if this is a blank line then maintain it
        // (i.e. try to preserve original paragraph breaks)
        // unless they occur at the very beginning of the text
-       if (($body{$pos} == "\n") && (strlen($outString) != 0)) {
+       if (($body{$pos} == "\n" ) && (strlen($outString) != 0)) {
            $outStringLast = $outString{strlen($outString) - 1};
            if ($outStringLast != "\n") {
                $outString .= "\n";
@@ -171,7 +209,10 @@ function &sqBodyWrap (&$body, $wrap) {
            $pos = $nextNewline + 1;
            continue;
        }
-
+       /**
+        * Set this to false to stop appending short strings to previous lines
+        */
+       $smartwrap = true;
        // inner loop, (obviously) handles wrapping up to
        // the next newline
        while ($pos < $nextNewline) {
@@ -179,20 +220,60 @@ function &sqBodyWrap (&$body, $wrap) {
            while (($pos < $nextNewline) && (ctype_space ($body{$pos}))) {
                $pos++;
            }
-
            // if this is a short line then just append it and continue outer loop
-           if (($outStringCol + $nextNewline - $pos) <= ($wrap - $citeLevel - 1)) {
+           if (($outStringCol + $nextNewline - $pos) <= ($wrap - $citeLevel - 1) ) {
                // if this is the final line in the input string then include
                // any trailing newlines
+               //      echo substr($body,$pos,$wrap). "<br />";
                if (($nextNewline + 1 == $length) && ($body{$nextNewline} == "\n")) {
                    $nextNewline++;
                }
 
                // trim trailing spaces
                $lastRealChar = $nextNewline;
-               while (($lastRealChar > $pos) && (ctype_space ($body{$lastRealChar}))) {
+               while (($lastRealChar > $pos && $lastRealChar < $length) && (ctype_space ($body{$lastRealChar}))) {
                    $lastRealChar--;
                }
+               // decide if appending the short string is what we want
+               if (($nextNewline < $length && $body{$nextNewline} == "\n") &&
+                     isset($lastRealChar)) {
+                   $mypos = $pos;
+                   //check the first word:
+                   while (($mypos < $length) && ($body{$mypos} == '>')) {
+                       $mypos++;
+                       // skip over any spaces interleaved among the cite markers
+                       $oldpos = $mypos;
+                       while (($mypos < $length) && ($body{$mypos} == ' ')) {
+                           $mypos++;
+                       }
+                   }
+/*
+                     $ldnspacecnt = 0;
+                     if ($mypos == $nextNewline+1) {
+                        while (($mypos < $length) && ($body{$mypos} == ' ')) {
+                         $ldnspacecnt++;
+                        }
+                     }
+*/
+
+                   $firstword = substr($body,$mypos,strpos($body,' ',$mypos) - $mypos);
+                   //if ($dowrap || $ldnspacecnt > 1 || ($firstword && (
+                   if (!$smartwrap || $firstword && (
+                                        $firstword{0} == '-' ||
+                                        $firstword{0} == '+' ||
+                                        $firstword{0} == '*' ||
+                                        $firstword{0} == strtoupper($firstword{0}) ||
+                                        strpos($firstword,':'))) {
+                        $outString .= substr($body,$pos,($lastRealChar - $pos+1));
+                        $outStringCol += ($lastRealChar - $pos);
+                        sqMakeNewLine($outString,$citeLevel,$outStringCol);
+                        $nextNewline++;
+                        $pos = $nextNewline;
+                        $outStringCol--;
+                        continue;
+                   }
+
+               }
 
                $outString .= substr ($body, $pos, ($lastRealChar - $pos + 1));
                $outStringCol += ($lastRealChar - $pos);
@@ -207,7 +288,7 @@ function &sqBodyWrap (&$body, $wrap) {
            // our current line is already too long, break immediately
            // and restart outer loop
            if ($eol <= $pos) {
-               sqMakeNewLine ($outString, $citeLeve, $outStringCol);
+               sqMakeNewLine ($outString, $citeLevel, $outStringCol);
                continue;
            }