]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/common/rbbinode.cpp
ICU-62123.0.1.tar.gz
[apple/icu.git] / icuSources / common / rbbinode.cpp
index f3a0a67911eec18f7fe69bf1e6f284136030c329..69d84151fe818a43665d1932a7ae3eb5f5c5db3c 100644 (file)
@@ -1,6 +1,8 @@
+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
 /*
 ***************************************************************************
-*   Copyright (C) 2002-2003 International Business Machines Corporation   *
+*   Copyright (C) 2002-2016 International Business Machines Corporation   *
 *   and others. All rights reserved.                                      *
 ***************************************************************************
 */
@@ -23,6 +25,8 @@
 #include "unicode/uniset.h"
 #include "unicode/uchar.h"
 #include "unicode/parsepos.h"
+
+#include "cstr.h"
 #include "uvector.h"
 
 #include "rbbirb.h"
@@ -33,8 +37,9 @@
 
 U_NAMESPACE_BEGIN
 
-int  RBBINode::gLastSerial = 0;
-
+#ifdef RBBI_DEBUG
+static int  gLastSerial = 0;
+#endif
 
 
 //-------------------------------------------------------------------------
@@ -43,7 +48,9 @@ int  RBBINode::gLastSerial = 0;
 //
 //-------------------------------------------------------------------------
 RBBINode::RBBINode(NodeType t) : UMemory() {
+#ifdef RBBI_DEBUG
     fSerialNum    = ++gLastSerial;
+#endif
     fType         = t;
     fParent       = NULL;
     fLeftChild    = NULL;
@@ -53,8 +60,10 @@ RBBINode::RBBINode(NodeType t) : UMemory() {
     fLastPos      = 0;
     fNullable     = FALSE;
     fLookAheadEnd = FALSE;
+    fRuleRoot     = FALSE;
+    fChainIn      = FALSE;
     fVal          = 0;
-       fPrecedence   = precZero;
+    fPrecedence   = precZero;
 
     UErrorCode     status = U_ZERO_ERROR;
     fFirstPosSet  = new UVector(status);  // TODO - get a real status from somewhere
@@ -69,7 +78,9 @@ RBBINode::RBBINode(NodeType t) : UMemory() {
 
 
 RBBINode::RBBINode(const RBBINode &other) : UMemory(other) {
+#ifdef RBBI_DEBUG
     fSerialNum   = ++gLastSerial;
+#endif
     fType        = other.fType;
     fParent      = NULL;
     fLeftChild   = NULL;
@@ -81,6 +92,8 @@ RBBINode::RBBINode(const RBBINode &other) : UMemory(other) {
     fLastPos     = other.fLastPos;
     fNullable    = other.fNullable;
     fVal         = other.fVal;
+    fRuleRoot    = FALSE;
+    fChainIn     = other.fChainIn;
     UErrorCode     status = U_ZERO_ERROR;
     fFirstPosSet = new UVector(status);   // TODO - get a real status from somewhere
     fLastPosSet  = new UVector(status);
@@ -144,13 +157,16 @@ RBBINode *RBBINode::cloneTree() {
         n = this;
     } else {
         n = new RBBINode(*this);
-        if (fLeftChild != NULL) {
-            n->fLeftChild          = fLeftChild->cloneTree();
-            n->fLeftChild->fParent = n;
-        }
-        if (fRightChild != NULL) {
-            n->fRightChild          = fRightChild->cloneTree();
-            n->fRightChild->fParent = n;
+        // Check for null pointer.
+        if (n != NULL) {
+            if (fLeftChild != NULL) {
+                n->fLeftChild          = fLeftChild->cloneTree();
+                n->fLeftChild->fParent = n;
+            }
+            if (fRightChild != NULL) {
+                n->fRightChild          = fRightChild->cloneTree();
+                n->fRightChild->fParent = n;
+            }
         }
     }
     return n;
@@ -178,8 +194,12 @@ RBBINode *RBBINode::cloneTree() {
 //-------------------------------------------------------------------------
 RBBINode *RBBINode::flattenVariables() {
     if (fType == varRef) {
-        RBBINode *retNode = fLeftChild->cloneTree();
-        delete this;
+        RBBINode *retNode  = fLeftChild->cloneTree();
+        if (retNode != NULL) {
+            retNode->fRuleRoot = this->fRuleRoot;
+            retNode->fChainIn  = this->fChainIn;
+        }
+        delete this;   // TODO: undefined behavior. Fix.
         return retNode;
     }
 
@@ -263,8 +283,14 @@ void   RBBINode::findNodes(UVector *dest, RBBINode::NodeType kind, UErrorCode &s
 //    print.         Print out a single node, for debugging.
 //
 //-------------------------------------------------------------------------
-void RBBINode::print() {
 #ifdef RBBI_DEBUG
+
+static int32_t serial(const RBBINode *node) {
+    return (node == NULL? -1 : node->fSerialNum);
+}
+
+
+void RBBINode::printNode(const RBBINode *node) {
     static const char * const nodeTypeNames[] = {
                 "setRef",
                 "uset",
@@ -284,30 +310,26 @@ void RBBINode::print() {
                 "opLParen"
     };
 
-    RBBIDebugPrintf("%10p  %12s  %10p  %10p  %10p      %4d     %6d   %d ",
-        (void *)this, nodeTypeNames[fType], (void *)fParent, (void *)fLeftChild, (void *)fRightChild,
-        fSerialNum, fFirstPos, fVal);
-    if (fType == varRef) {
-        printUnicodeString(fText);
+    if (node==NULL) {
+        RBBIDebugPrintf("%10p", (void *)node);
+    } else {
+        RBBIDebugPrintf("%10p %5d %12s %c%c  %5d       %5d     %5d       %6d     %d ",
+            (void *)node, node->fSerialNum, nodeTypeNames[node->fType],
+            node->fRuleRoot?'R':' ', node->fChainIn?'C':' ',
+            serial(node->fLeftChild), serial(node->fRightChild), serial(node->fParent),
+            node->fFirstPos, node->fVal);
+        if (node->fType == varRef) {
+            RBBI_DEBUG_printUnicodeString(node->fText);
+        }
     }
     RBBIDebugPrintf("\n");
-#endif
 }
+#endif
 
 
 #ifdef RBBI_DEBUG
-void RBBINode::printUnicodeString(const UnicodeString &, int) {}
-#else
-void RBBINode::printUnicodeString(const UnicodeString &s, int minWidth)
-{
-    int i;
-    for (i=0; i<s.length(); i++) {
-        RBBIDebugPrintf("%c", s.charAt(i));
-        // putc(s.charAt(i), stdout);
-    }
-    for (i=s.length(); i<minWidth; i++) {
-        RBBIDebugPrintf(" ");
-    }
+U_CFUNC void RBBI_DEBUG_printUnicodeString(const UnicodeString &s, int minWidth) {
+    RBBIDebugPrintf("%*s", minWidth, CStr(s)());
 }
 #endif
 
@@ -318,24 +340,26 @@ void RBBINode::printUnicodeString(const UnicodeString &s, int minWidth)
 //
 //-------------------------------------------------------------------------
 #ifdef RBBI_DEBUG
-void RBBINode::printTree(UBool, UBool) {}
-#else
-void RBBINode::printTree(UBool printHeading, UBool doVars) {
+void RBBINode::printNodeHeader() {
+    RBBIDebugPrintf(" Address   serial        type     LeftChild  RightChild   Parent   position value\n");
+}
+    
+void RBBINode::printTree(const RBBINode *node, UBool printHeading) {
     if (printHeading) {
-        RBBIDebugPrintf( "-------------------------------------------------------------------\n"
-                         "    Address       type         Parent   LeftChild  RightChild    serial  position value\n"
-              );
+        printNodeHeader();
     }
-    this->print();
-    // Only dump the definition under a variable reference if asked to.
-    // Unconditinally dump children of all other node types.
-    if (fType != varRef || doVars) {
-        if (fLeftChild != NULL) {
-            fLeftChild->printTree(FALSE);
-        }
-
-        if (fRightChild != NULL) {
-            fRightChild->printTree(FALSE);
+    printNode(node);
+    if (node != NULL) {
+        // Only dump the definition under a variable reference if asked to.
+        // Unconditinally dump children of all other node types.
+        if (node->fType != varRef) {
+            if (node->fLeftChild != NULL) {
+                printTree(node->fLeftChild, FALSE);
+            }
+            
+            if (node->fRightChild != NULL) {
+                printTree(node->fRightChild, FALSE);
+            }
         }
     }
 }