Concat sourcemaps and keep mappings intact

This is implemented very efficent by avoiding to parse the whole mappings string.
164988
public function concat(Kwf_SourceMaps_SourceMap $other)
    {
        if ($this->_mappingsChanged) {
          $this->_generateMappings();
        }
        $missingLines = substr_count($this->_fileContents, "\n")-substr_count($this->_map->mappings, ";");
        if ($missingLines > 0) {
            $this->_map->mappings .= str_repeat(';', $missingLines);
        }
        if (!isset($this->_map->{'_x_org_koala-framework_last'})) {
            $this->_addLastExtension();
        }

        if (strlen($this->_fileContents) > 0) {
            if (substr($this->_fileContents, -1) != "\n") {
                $this->_fileContents .= "\n";
                $this->_map->mappings .= ';';
            }
        }

        $this->_fileContents .= $other->_fileContents;

        $data = $other->getMapContentsData();

        if ($this->_map->mappings) {
            $previousFileLast = $this->_map->{'_x_org_koala-framework_last'};
        } else {
            $previousFileLast = (object) array(
                'source' => 0,
                'originalLine' => 0,
                'originalColumn' => 0,
                'name' => 0,
            );
        }
        if (!$data->mappings) {
            $data->mappings = str_repeat(';', substr_count($other->_fileContents, "\n"));
            $data->{'_x_org_koala-framework_last'} = (object) array(
                'source' => -1,
                'originalLine' => $previousFileLast->originalLine,
                'originalColumn' => $previousFileLast->originalColumn,
                'name' => -1,
            );
        }
        $previousFileSourcesCount = count($this->_map->sources);
        $previousFileNamesCount = count($this->_map->names);
        if ($previousFileLast->source > $previousFileSourcesCount) {
            if ($previousFileSourcesCount != 0 && $previousFileLast->source != 0) {
                throw new Exception("Invalid last source, must not be higher than sources");
            }
        }

        if ($previousFileLast->name > $previousFileNamesCount) {
            if ($previousFileNamesCount != 0 && $previousFileLast->name != 0) {
                throw new Exception("Invalid last name, must not be higher than names");
            }
        }

        if ($data->sources) {
            foreach ($data->sources as $s) {
                $this->_map->sources[] = $s;
            }
        }
        if ($data->names) {
            foreach ($data->names as $n) {
                $this->_map->names[] = $n;
            }
        }

        $otherMappings = $data->mappings;

        $str = '';

        $otherMappingsEnd = strlen($otherMappings);
        $otherMappingsPos = 0;

        while ($otherMappingsPos < $otherMappingsEnd && $otherMappings[$otherMappingsPos] === ';') {
            $str .= $otherMappings[$otherMappingsPos];
            $otherMappingsPos++;
        }
        if ($otherMappingsPos < $otherMappingsEnd) {

            // Generated column.
            $str .= Kwf_SourceMaps_Base64VLQ::encode(Kwf_SourceMaps_Base64VLQ::decodePos($otherMappings, $otherMappingsPos));
            if ($otherMappingsPos < $otherMappingsEnd && !($otherMappings[$otherMappingsPos] == ',' || $otherMappings[$otherMappingsPos] == ';')) {

                // Original source.
                $value = Kwf_SourceMaps_Base64VLQ::decodePos($otherMappings, $otherMappingsPos);
                if ($previousFileSourcesCount) {
                    $absoluteValue = $value + $previousFileSourcesCount;
                    $value = $absoluteValue - $previousFileLast->source;
                }
                $str  .= Kwf_SourceMaps_Base64VLQ::encode($value);

                // Original line.
                $str  .= Kwf_SourceMaps_Base64VLQ::encode(Kwf_SourceMaps_Base64VLQ::decodePos($otherMappings, $otherMappingsPos) - $previousFileLast->originalLine);

                // Original column.
                $str  .= Kwf_SourceMaps_Base64VLQ::encode(Kwf_SourceMaps_Base64VLQ::decodePos($otherMappings, $otherMappingsPos) - $previousFileLast->originalColumn);

                // Original name.
                if ($otherMappingsPos < $otherMappingsEnd && !($otherMappings[$otherMappingsPos] == ',' || $otherMappings[$otherMappingsPos] == ';')) {
                    $value = Kwf_SourceMaps_Base64VLQ::decodePos($otherMappings, $otherMappingsPos);
                    if ($previousFileNamesCount) {
                        $absoluteValue = $value + $previousFileNamesCount;
                        $value = $absoluteValue - $previousFileLast->name;
                    }
                    $str .= Kwf_SourceMaps_Base64VLQ::encode($value);
                } else if (!count($data->names)) {
                    //file doesn't have names at all, we don't have to adjust that offset
                } else {
                    //loop thru mappings until we find a block with name
                    while ($otherMappingsPos < $otherMappingsEnd) {
                        if ($otherMappings[$otherMappingsPos] === ';') {
                            $str .= $otherMappings[$otherMappingsPos];
                            $otherMappingsPos++;
                        } else if ($otherMappings[$otherMappingsPos] === ',') {
                            $str .= $otherMappings[$otherMappingsPos];
                            $otherMappingsPos++;
                        } else {
                            // Generated column.
                            $str .= Kwf_SourceMaps_Base64VLQ::encode(Kwf_SourceMaps_Base64VLQ::decodePos($otherMappings, $otherMappingsPos));

                            if ($otherMappingsPos < $otherMappingsEnd && !($otherMappings[$otherMappingsPos] == ',' || $otherMappings[$otherMappingsPos] == ';')) {
                                // Original source.
                                $str .= Kwf_SourceMaps_Base64VLQ::encode(Kwf_SourceMaps_Base64VLQ::decodePos($otherMappings, $otherMappingsPos));

                                // Original line.
                                $str .= Kwf_SourceMaps_Base64VLQ::encode(Kwf_SourceMaps_Base64VLQ::decodePos($otherMappings, $otherMappingsPos));

                                // Original column.
                                $str .= Kwf_SourceMaps_Base64VLQ::encode(Kwf_SourceMaps_Base64VLQ::decodePos($otherMappings, $otherMappingsPos));

                                if ($otherMappingsPos < $otherMappingsEnd && !($otherMappings[$otherMappingsPos] == ',' || $otherMappings[$otherMappingsPos] == ';')) {
                                    // Original name.
                                    $value = Kwf_SourceMaps_Base64VLQ::decodePos($otherMappings, $otherMappingsPos);
                                    if ($previousFileNamesCount) {
                                        $absoluteValue = $value + $previousFileNamesCount;
                                        $value = $absoluteValue - $previousFileLast->name;
                                    }
                                    $str .= Kwf_SourceMaps_Base64VLQ::encode($value);
                                    break;
                                }
                            }
                        }
                    }
                }
            }

        }

        $this->_map->mappings .= $str.substr($otherMappings, $otherMappingsPos);

        $this->_map->{'_x_org_koala-framework_last'} = (object) array(
            'source' => $previousFileSourcesCount + $data->{'_x_org_koala-framework_last'}->source,
            'name' => $previousFileNamesCount + $data->{'_x_org_koala-framework_last'}->name,
            'originalLine' => $data->{'_x_org_koala-framework_last'}->originalLine,
            'originalColumn' => $data->{'_x_org_koala-framework_last'}->originalColumn
        );
    }
1002814
private synchronized void addSourceMapSourceFiles(SourceMapInput inputSourceMap) {
    // synchronized annotation guards concurrent access to sourceMap during parsing.
    SourceMapConsumerV3 consumer = inputSourceMap.getSourceMap(errorManager);
    if (consumer == null) {
      return;
    }
    Collection<String> sourcesContent = consumer.getOriginalSourcesContent();
    if (sourcesContent == null) {
      return;
    }
    Iterator<String> content = sourcesContent.iterator();
    Iterator<String> sources = consumer.getOriginalSources().iterator();
    while (sources.hasNext() && content.hasNext()) {
      String code = content.next();
      SourceFile source = SourceMapResolver.getRelativePath(
          inputSourceMap.getOriginalPath(), sources.next());
      if (source != null) {
        sourceMap.addSourceFile(source.getOriginalPath(), code);
      }
    }
    if (sources.hasNext() || content.hasNext()) {
      throw new RuntimeException(
          "Source map's \"sources\" and \"sourcesContent\" lengths do not match.");
    }
  }
890666
def map_copy(source: tcod.map.Map, dest: tcod.map.Map) -> None:
    
    if source.width != dest.width or source.height != dest.height:
        dest.__init__(  # type: ignore
            source.width, source.height, source._order
        )
    dest._Map__buffer[:] = source._Map__buffer[:]
1037361
def parse_mappings(mappings, key_rvalue=False):
    
    parsed = {}
    if mappings:
        mappings = mappings.split()
        for m in mappings:
            p = m.partition(':')

            if key_rvalue:
                key_index = 2
                val_index = 0
                # if there is no rvalue skip to next
                if not p[1]:
                    continue
            else:
                key_index = 0
                val_index = 2

            key = p[key_index].strip()
            parsed[key] = p[val_index].strip()

    return parsed
1065404
private static int mergeMaps(Map<String, Integer> map, Map<String, Integer> other)
    {
        int deltaSize = 0;
        for (Map.Entry<String, Integer> entry : other.entrySet()) {
            if (!map.containsKey(entry.getKey())) {
                deltaSize += entry.getKey().getBytes().length + SIZE_OF_INT;
            }
            map.put(entry.getKey(), map.getOrDefault(entry.getKey(), 0) + other.getOrDefault(entry.getKey(), 0));
        }
        return deltaSize;
    }
419684
@SuppressWarnings("unchecked")
	private void mergeMap (Map<String, Object> dest, Object source) throws YamlReaderException {
		if (source instanceof Collection) {
			for (Object item : ((Collection<Object>)source))
				mergeMap(dest, item);
		} else if (source instanceof Map) {
			Map<String, Object> map = (Map<String, Object>)source;
			for (Map.Entry<String, Object> entry : map.entrySet()) {
				if (!dest.containsKey(entry.getKey())) dest.put(entry.getKey(), entry.getValue());

			}
		} else
			throw new YamlReaderException("Expected a mapping or a sequence of mappings for a '<<' merge field but found: "
				+ source.getClass().getSimpleName());

	}
256060
public function mergeMapWithBinded( $map ) {

		foreach( $map as $key => $value ) {

			if( !in_array( $key, $this->binded ) ) {

				$this->binded[] = $key;
				$this->value[]	= $value;
			}
		}
	}
756108
public JcString concat(JcString concat) {
		JcString ret = new JcString(concat, this, OPERATOR.String.CONCAT);
		QueryRecorder.recordInvocationConditional(this, "concat", ret, QueryRecorder.placeHolder(concat));
		return ret;
	}
557420
def _merge_maps(m1, m2):
    """"""
    return type(m1)(chain(m1.items(), m2.items()))
374757
static private void astifyMap(StringBuilder strb, java.util.Map<?,?> map) {
    strb.append("{");
    if (map != null && map.size() > 0) {
      boolean isFirst = true;
      for (java.util.Map.Entry<?, ?> entry: map.entrySet()) {
        if (isFirst) {
          isFirst = false;
        } else {
          strb.append(", ");
        }
        strb.append("\"");
        strb.append(entry.getKey().toString());
        strb.append("\"");
        strb.append(":");
        astifyObject(strb, entry.getValue());
      }
    }
    strb.append("}");
  }
353154
public String concatQueryString(Map<String, String> parameters)
        throws UnsupportedEncodingException {
        if (null == parameters) {
            return null;
        }

        StringBuilder urlBuilder = new StringBuilder("");
        for (Map.Entry<String, String> entry : parameters.entrySet()) {
            String key = entry.getKey();
            String val = entry.getValue();
            urlBuilder.append(encode(key));
            if (val != null) {
                urlBuilder.append("=").append(encode(val));
            }
            urlBuilder.append("&");
        }

        int strIndex = urlBuilder.length();
        if (parameters.size() > 0) {
            urlBuilder.deleteCharAt(strIndex - 1);
        }

        return urlBuilder.toString();
    }
902562
protected void flatCopyTo(String prefix, Map<String, Object> sourceMap, HttpHeaders headers) {
        for (Map.Entry<String, Object> entry : sourceMap.entrySet()) {
            String key = prefix + entry.getKey();
            Object value = entry.getValue();
            if (value instanceof String) {
                addToHeader(headers, key, (CharSequence) value);
            } else if (value instanceof Number) {
                addToHeader(headers, key, value.toString());
            } else if (value instanceof Map) {
                flatCopyTo(key + ".", (Map<String, Object>) value, headers);
            }
        }
    }
240663
def append(self, map):
        
        if isinstance(map, dict):
            for (k, v) in map.items():
                key = StringConverter.to_string(k)
                value = v
                self.put(key, value)
507252
public static StringValueMap fromMaps(Map<?, ?>... maps) {
		StringValueMap result = new StringValueMap();
		if (maps != null && maps.length > 0) {
			for (Map<?, ?> map : maps)
				result.append(map);
		}
		return result;
	}
1077368
public static <X, Y, Z> Map<X, Z> compose(Map<X, Y> map1, Map<Y, Z> map2) {
    Map<X, Z> composedMap = new HashMap<X, Z>();
    for (X key : map1.keySet()) {
      composedMap.put(key, map2.get(map1.get(key)));
    }
    return composedMap;
  }
1324146
public void concatField(String targetField, String concatString, String... sourceFields) {
        TransformationStep step = new TransformationStep();
        step.setTargetField(targetField);
        step.setSourceFields(sourceFields);
        step.setOperationParameter(TransformationConstants.CONCAT_PARAM, concatString);
        step.setOperationName("concat");
        steps.add(step);
    }
429752
@Override
    public <R> Future<R> concatMap(final Function<? super T, ? extends Iterable<? extends R>> mapper) {
        return (Future<R>) MonadicValue.super.concatMap(mapper);
    }