548 def format_value(self, value, check=True):
549 """Format value change for VCD stream.
550
551 :param value: New value for the variable.
552 :types value: int, str, or None
553 :raises ValueError: for *some* invalid values.
554
555 A *value* of `None` is the same as `'z'`.
556
557 .. Warning::
558
559 If *value* is of type :py:class:`str`, all characters must be one
560 of `'01xzXZ'`. For the sake of performance, checking **is not**
561 done to ensure value strings only contain conforming characters.
562 Thus it is possible to produce invalid VCD streams with invalid
563 string values.
564
565 """
566 if isinstance(self.size, tuple):
567
568
569 vstr_list = []
570 vstr_len = 0
571 size_sum = 0
572 for i, (v, size) in enumerate(zip(reversed(value),
573 reversed(self.size))):
574 vstr = self._format_value(v, size, check)
575 if not vstr_list:
576 vstr_list.insert(0, vstr)
577 vstr_len += len(vstr)
578 else:
579 leftc = vstr_list[0][0]
580 rightc = vstr[0]
581 if len(vstr) > 1 or ((rightc != leftc or leftc == '1') and
582 (rightc != '0' or leftc != '1')):
583 extendc = '0' if leftc == '1' else leftc
584 extend_size = size_sum - vstr_len
585 vstr_list.insert(0, extendc * extend_size)
586 vstr_list.insert(0, vstr)
587 vstr_len += extend_size + len(vstr)
588 size_sum += size
589 value_str = ''.join(vstr_list)
590 else:
591 value_str = self._format_value(value, self.size, check)
592 return f'b{value_str} {self.ident}'
593