                         }
                     }
                 } else if char == "}" {
+                    // print("#2 choice: char = \(char)")
+
                     // continue normal pattern matching
-                    
                     if pi == pattern.endIndex && si == address.endIndex {
                         return true
                     }
 
                     si = address.index(before: si) // str is incremented again below
-                    break
                 } else if char == address[si] {
+                    // print("#3 choice: \(char) == \(address[si])")
                     si = address.index(after: si)
                     if si == address.endIndex && remainder < pattern.endIndex {
-                        return false
-                    }
-                    // step back
-                    si = address.index(before: si)
+                        print("ERROR: address == EOF, pattern != EOF")
+                        return false
+                    }
                 } else { // skip to next comma
+                    // print("#4 choice: char = \(char)")
+
+                    // reset string position
                     si = place
+
                     while pi < pattern.endIndex && pattern[pi] != "," && pattern[pi] != "}" {
                         pi = pattern.index(after: pi)
                     }
-                    if pi < pattern.endIndex {
-                        if pattern[pi] == "," {
-                            pi = pattern.index(after: pi)
-                        } else if pattern[pi] == "}" {
-                            return false
-                        }
-                    }
-                }
-
-                // end of while
+                    
+                    if pi == pattern.endIndex {
+                        print("ERR: UNBALANCED CHOICE")
+                        return false
+                    }
+
+                    if pattern[pi] == "," {
+                        pi = pattern.index(after: pi)
+                    } else if pattern[pi] == "}" {
+                        return false
+                    }
+                }
+
+                // --- end of inner switch ---
                 char = pattern[pi]
-                pi = pattern.index(after:pi)
-            }
-
-            
-            break
+                if pi < pattern.endIndex {
+                    pi = pattern.index(after:pi)
+                }
+            }
         default:
             if char != address[si] {
                 return false
             }
-
-            break
         }
 
-        si = address.index(after:si)
+        // --- end of outer switch ---
+        
+        if si < address.endIndex {
+            si = address.index(after:si)
+        }
         
     }
     

//: Playground - noun: a place where people can play

import Cocoa


/*
 */
func matches(address: String, pattern: String) -> Bool {
    // var negate = false

}


print("true", matches(address:"ablak", pattern:"ablak"))

print("false", matches(address:"ablak", pattern:"abrak"))

print("true", matches(address:"ablak", pattern:"ab?ak"))

print("true", matches(address:"ablak", pattern:"ab*"))
print("true", matches(address:"ablak", pattern:"ab*k"))

print("true", matches(address:"ablak", pattern:"a{blak}"))
print("true", matches(address:"ablak", pattern:"a{blak,jto}"))
