@@ -97,7 +97,7 @@ import Control.Exception ( IOException
9797import Data.IORef (IORef, newIORef, writeIORef, readIORef)
9898import Data.ByteString (ByteString)
9999import qualified Data.ByteString as BS (concat)
100−import qualified Data.ByteString.Char8 as BSC (pack, unpack, split)
100+import qualified Data.ByteString.Char8 as BSC (pack, unpack)
101101import Data.Int (Int32)
102102import Data.IntMap (IntMap)
103103import qualified Data.IntMap as IntMap (empty)
@@ -1539,7 +1539,7 @@ encodeEndPointAddress host port ix = EndPointAddress . BSC.pack $
15391539decodeEndPointAddress :: EndPointAddress
15401540 -> Maybe (N.HostName, N.ServiceName, EndPointId)
15411541decodeEndPointAddress (EndPointAddress bs) =
1542− case map BSC.unpack $ BSC.split ':' bs of
1542+ case splitMaxFromEnd (== ':') 2 $ BSC.unpack bs of
15431543 [host, port, endPointIdStr] ->
15441544 case reads endPointIdStr of
15451545 [(endPointId, "")] -> Just (host, port, endPointId)
@@ -1547,6 +1547,21 @@ decodeEndPointAddress (EndPointAddress bs) =
15471547 _ ->
15481548 Nothing
15491549
1550+-- | @spltiMaxFromEnd p n xs@ splits list @xs@ at elements matching @p@,
1551+-- returning at most @p@ segments -- counting from the /end/
1552+--
1553+-- > splitMaxFromEnd (== ':') 2 "ab:cd:ef:gh" == ["ab:cd", "ef", "gh"]
1554+splitMaxFromEnd :: (a -> Bool) -> Int -> [a] -> [[a]]
1555+splitMaxFromEnd p = \n -> go [[]] n . reverse
1556+ where
1557+ -- go :: [[a]] -> Int -> [a] -> [[a]]
1558+ go accs _ [] = accs
1559+ go ([] : accs) 0 xs = reverse xs : accs
1560+ go (acc : accs) n (x:xs) =
1561+ if p x then go ([] : acc : accs) (n - 1) xs
1562+ else go ((x : acc) : accs) n xs
1563+ go _ _ _ = error "Bug in splitMaxFromEnd"
1564+
15501565--------------------------------------------------------------------------------
15511566-- Functions from TransportInternals --
15521567--------------------------------------------------------------------------------