mercurial/utils/cborutil.py
changeset 52644 e627cc25b6f3
parent 52634 fb8932685031
child 52691 279e217d6041
equal deleted inserted replaced
52643:5cc8deb96b48 52644:e627cc25b6f3
   135     """Encode a known size iterable to an array."""
   135     """Encode a known size iterable to an array."""
   136 
   136 
   137     yield encodelength(MAJOR_TYPE_ARRAY, len(l))
   137     yield encodelength(MAJOR_TYPE_ARRAY, len(l))
   138 
   138 
   139     for i in l:
   139     for i in l:
   140         for chunk in streamencode(i):
   140         yield from streamencode(i)
   141             yield chunk
       
   142 
   141 
   143 
   142 
   144 def streamencodearrayfromiter(it):
   143 def streamencodearrayfromiter(it):
   145     """Encode an iterator of items to an indefinite length array."""
   144     """Encode an iterator of items to an indefinite length array."""
   146 
   145 
   147     yield BEGIN_INDEFINITE_ARRAY
   146     yield BEGIN_INDEFINITE_ARRAY
   148 
   147 
   149     for i in it:
   148     for i in it:
   150         for chunk in streamencode(i):
   149         yield from streamencode(i)
   151             yield chunk
       
   152 
   150 
   153     yield BREAK
   151     yield BREAK
   154 
   152 
   155 
   153 
   156 def _mixedtypesortkey(v):
   154 def _mixedtypesortkey(v):
   160 def streamencodeset(s):
   158 def streamencodeset(s):
   161     # https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml defines
   159     # https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml defines
   162     # semantic tag 258 for finite sets.
   160     # semantic tag 258 for finite sets.
   163     yield encodelength(MAJOR_TYPE_SEMANTIC, SEMANTIC_TAG_FINITE_SET)
   161     yield encodelength(MAJOR_TYPE_SEMANTIC, SEMANTIC_TAG_FINITE_SET)
   164 
   162 
   165     for chunk in streamencodearray(sorted(s, key=_mixedtypesortkey)):
   163     yield from streamencodearray(sorted(s, key=_mixedtypesortkey))
   166         yield chunk
       
   167 
   164 
   168 
   165 
   169 def streamencodemap(d):
   166 def streamencodemap(d):
   170     """Encode dictionary to a generator.
   167     """Encode dictionary to a generator.
   171 
   168 
   172     Does not supporting indefinite length dictionaries.
   169     Does not supporting indefinite length dictionaries.
   173     """
   170     """
   174     yield encodelength(MAJOR_TYPE_MAP, len(d))
   171     yield encodelength(MAJOR_TYPE_MAP, len(d))
   175 
   172 
   176     for key, value in sorted(d.items(), key=lambda x: _mixedtypesortkey(x[0])):
   173     for key, value in sorted(d.items(), key=lambda x: _mixedtypesortkey(x[0])):
   177         for chunk in streamencode(key):
   174         yield from streamencode(key)
   178             yield chunk
   175         yield from streamencode(value)
   179         for chunk in streamencode(value):
       
   180             yield chunk
       
   181 
   176 
   182 
   177 
   183 def streamencodemapfromiter(it):
   178 def streamencodemapfromiter(it):
   184     """Given an iterable of (key, value), encode to an indefinite length map."""
   179     """Given an iterable of (key, value), encode to an indefinite length map."""
   185     yield BEGIN_INDEFINITE_MAP
   180     yield BEGIN_INDEFINITE_MAP
   186 
   181 
   187     for key, value in it:
   182     for key, value in it:
   188         for chunk in streamencode(key):
   183         yield from streamencode(key)
   189             yield chunk
   184         yield from streamencode(value)
   190         for chunk in streamencode(value):
       
   191             yield chunk
       
   192 
   185 
   193     yield BREAK
   186     yield BREAK
   194 
   187 
   195 
   188 
   196 def streamencodebool(b):
   189 def streamencodebool(b):