/*
(non-Javadoc)

@see
de.tud.ukp.kulessa.delta.data.codec.RevisionEncoderInterface#encodeDiff
(de.tud.ukp.kulessa.delta.data.codec.RevisionCodecData,
de.tud.ukp.kulessa.delta.data.tasks.content.Diff)
179752
@Override
	public String encodeDiff(final RevisionCodecData codecData, final Diff diff)
		throws UnsupportedEncodingException, EncodingException
	{

		String sEncoding;
		byte[] bData = encode(codecData, diff);
		if (MODE_ZIP_COMPRESSION) {

			Deflater compresser = new Deflater();
			compresser.setInput(bData);
			compresser.finish();

			byte[] output = new byte[1000];
			ByteArrayOutputStream stream = new ByteArrayOutputStream();

			int cLength;
			do {
				cLength = compresser.deflate(output);
				stream.write(output, 0, cLength);
			}
			while (cLength == 1000);

			output = stream.toByteArray();

			if (bData.length + 1 < output.length) {
				sEncoding = Base64.encodeBase64String(bData);
			}
			else {
				sEncoding = "_" + Base64.encodeBase64String(output);
			}
		}
		else {
			sEncoding = Base64.encodeBase64String(bData);
		}

		return sEncoding;
	}
389910
@Override
    synchronized public int encodeIntoCompressedByteBuffer(
            final ByteBuffer targetBuffer,
            final int compressionLevel) {
        int neededCapacity = getNeededByteBufferCapacity(countsArrayLength);
        if (intermediateUncompressedByteBuffer == null || intermediateUncompressedByteBuffer.capacity() < neededCapacity) {
            intermediateUncompressedByteBuffer = ByteBuffer.allocate(neededCapacity).order(BIG_ENDIAN);
        }
        intermediateUncompressedByteBuffer.clear();
        int initialTargetPosition = targetBuffer.position();

        final int uncompressedLength = encodeIntoByteBuffer(intermediateUncompressedByteBuffer);
        targetBuffer.putInt(getCompressedEncodingCookie());

        targetBuffer.putInt(0); // Placeholder for compressed contents length

        Deflater compressor = new Deflater(compressionLevel);
        compressor.setInput(intermediateUncompressedByteBuffer.array(), 0, uncompressedLength);
        compressor.finish();

        byte[] targetArray;

        if (targetBuffer.hasArray()) {
            targetArray = targetBuffer.array();
        } else {
            if (intermediateUncompressedByteArray == null ||
                intermediateUncompressedByteArray.length < targetBuffer.capacity()) {
                intermediateUncompressedByteArray = new byte[targetBuffer.capacity()];
            }
            targetArray = intermediateUncompressedByteArray;
        }

        int compressedTargetOffset = initialTargetPosition + 8;
        int compressedDataLength =
            compressor.deflate(
                targetArray,
                compressedTargetOffset,
                targetArray.length - compressedTargetOffset
            );
        compressor.end();

        if (!targetBuffer.hasArray()) {
            targetBuffer.put(targetArray, compressedTargetOffset, compressedDataLength);
        }

        targetBuffer.putInt(initialTargetPosition + 4, compressedDataLength); // Record the compressed length
        int bytesWritten = compressedDataLength + 8;
        targetBuffer.position(initialTargetPosition + bytesWritten);
        return bytesWritten;
    }
246826
public static String encodeQuery(String query, String encoding) throws UnsupportedEncodingException {
		return HierarchicalUriComponents.encodeUriComponent(query, encoding, HierarchicalUriComponents.Type.QUERY);
	}
749808
public void writeDiffFile(final Task<Diff> diff)
		throws IOException
	{

		int partsCount;
		int size = diff.size();
		Diff d;
		DiffPart p;
		RevisionCodecData codec;

		this.writer
				.write(WikipediaXMLKeys.KEY_START_PAGE.getKeyword() + "\r\n");

		ArticleInformation header = diff.getHeader();

		this.writer.write("\t" + WikipediaXMLKeys.KEY_START_TITLE.getKeyword());
		this.writer.write(header.getArticleName());
		this.writer.write(WikipediaXMLKeys.KEY_END_TITLE.getKeyword() + "\r\n");

		this.writer.write("\t" + WikipediaXMLKeys.KEY_START_ID.getKeyword());
		this.writer.write(Integer.toString(header.getArticleId()));
		this.writer.write(WikipediaXMLKeys.KEY_END_ID.getKeyword() + "\r\n");

		this.writer.write("\t<partCounter>");
		this.writer.write(Integer.toString(diff.getPartCounter()));
		this.writer.write("</partCounter>\r\n");

		for (int i = 0; i < size; i++) {
			d = diff.get(i);

			this.writer
					.write("\t"
							+ WikipediaXMLKeys.KEY_START_REVISION.getKeyword()
							+ "\r\n");

			codec = d.getCodecData();
			if (!codec.isConverted()) {
				codec.totalSizeInBits();
			}

			this.writer.write("\t\t<codecData>\r\n");

			this.writer.write("\t\t\t<s>"
					+ Integer.toString(codec.getBlocksizeS()) + "</s>\r\n");
			this.writer.write("\t\t\t<e>"
					+ Integer.toString(codec.getBlocksizeE()) + "</e>\r\n");
			this.writer.write("\t\t\t<b>"
					+ Integer.toString(codec.getBlocksizeB()) + "</b>\r\n");
			this.writer.write("\t\t\t<l>"
					+ Integer.toString(codec.getBlocksizeL()) + "</l>\r\n");

			this.writer.write("\t\t</codecData>\r\n");

			this.writer.write("\t\t"
					+ WikipediaXMLKeys.KEY_START_ID.getKeyword());
			this.writer.write(Integer.toString(d.getRevisionID()));
			this.writer
					.write(WikipediaXMLKeys.KEY_END_ID.getKeyword() + "\r\n");

			this.writer.write("\t\t<revCount>");
			this.writer.write(Integer.toString(d.getRevisionCounter()));
			this.writer.write("</revCount>\r\n");

			this.writer.write("\t\t"
					+ WikipediaXMLKeys.KEY_START_TIMESTAMP.getKeyword());
			this.writer.write(d.getTimeStamp().toString());
			this.writer.write(WikipediaXMLKeys.KEY_END_TIMESTAMP.getKeyword()
					+ "\r\n");

			this.writer.write("\t\t<diff>\r\n");
			partsCount = d.size();
			for (int j = 0; j < partsCount; j++) {

				p = d.get(j);
				this.writer.write("\t\t\t<diffPart>\r\n");

				this.writer.write("\t\t\t\t<action>" + p.getAction()
						+ "</action>\r\n");
				this.writer.write("\t\t\t\t<start>"
						+ Integer.toString(p.getStart()) + "</start>\r\n");
				this.writer.write("\t\t\t\t<end>"
						+ Integer.toString(p.getEnd()) + "</end>\r\n");
				if (p.getText() != null) {
					this.writer
							.write("\t\t\t\t<content xml:space=\"preserve\">"
									+ p.getText());
					this.writer.write("</content>\r\n");
				}

				this.writer.write("\t\t\t</diffPart>\r\n");
			}

			this.writer.write("\t\t</diff>\r\n");
			this.writer.write("\t"
					+ WikipediaXMLKeys.KEY_END_REVISION.getKeyword() + "\r\n");
		}

		this.writer.write(WikipediaXMLKeys.KEY_END_PAGE.getKeyword() + "\r\n");
		this.writer.flush();
	}
1370979
public StringBuffer encode(StringBuffer buff) {
		buff.append('(');
		buff.append(attribute).append(getCompareString()).append(encodedValue);
		buff.append(')');

		return buff;
	}
1307315
@Nullable
    protected String getEncodedTextDescription(int tagType)
    {
        byte[] commentBytes = _directory.getByteArray(tagType);
        if (commentBytes == null)
            return null;
        if (commentBytes.length == 0)
            return "";

        final Map<String, String> encodingMap = new HashMap<String, String>();
        encodingMap.put("ASCII", System.getProperty("file.encoding")); // Someone suggested "ISO-8859-1".
        encodingMap.put("UNICODE", "UTF-16LE");
        encodingMap.put("JIS", "Shift-JIS"); // We assume this charset for now.  Another suggestion is "JIS".

        try {
            if (commentBytes.length >= 10) {
                String firstTenBytesString = new String(commentBytes, 0, 10);

                // try each encoding name
                for (Map.Entry<String, String> pair : encodingMap.entrySet()) {
                    String encodingName = pair.getKey();
                    String charset = pair.getValue();
                    if (firstTenBytesString.startsWith(encodingName)) {
                        // skip any null or blank characters commonly present after the encoding name, up to a limit of 10 from the start
                        for (int j = encodingName.length(); j < 10; j++) {
                            byte b = commentBytes[j];
                            if (b != '\0' && b != ' ')
                                return new String(commentBytes, j, commentBytes.length - j, charset).trim();
                        }
                        return new String(commentBytes, 10, commentBytes.length - 10, charset).trim();
                    }
                }
            }
            // special handling fell through, return a plain string representation
            return new String(commentBytes, System.getProperty("file.encoding")).trim();
        } catch (UnsupportedEncodingException ex) {
            return null;
        }
    }
600736
public static String getDiff(String old, String newData, String identify, String htmlClick) {

        StringComparator stringComparator = new StringComparator(old, newData);
        String contentString = StringEscapeUtils.escapeHtml4(identify) + "<br/><br/>" + htmlClick + "<br/> ";

        try {

            if (stringComparator.getChangesFromOriginal().size() == 0 &&
                    stringComparator.getDeletesFromOriginal().size() == 0 &&
                    stringComparator.getInsertsFromOriginal().size() == 0) {

                return "<span style='color:#FF0000'>OK, NO MODIFICATOIN!</span>";

            } else {

                String oldValue = "<br/><br/><br/><span style='color:#FF0000'>Old value:</span><br/>" +
                        StringEscapeUtils.escapeHtml4(old).replaceAll("\n", "<br/>");

                String newValue = "<br/><br/><br/><span style='color:#FF0000'>New value:</span><br/>" +
                        StringEscapeUtils.escapeHtml4(newData).replaceAll("\n", "<br/>");

                String diff = "";
                if (stringComparator.getChangesFromOriginal().size() != 0) {
                    diff = "<span style='color:#FF0000'>Change info: </span><br/>";
                    for (Chunk chunk : stringComparator.getChangesFromOriginal()) {
                        diff += StringEscapeUtils.escapeHtml4(chunk.toString()) + "<br/>";
                    }
                }

                if (stringComparator.getInsertsFromOriginal().size() != 0) {
                    diff += "<br/><span style='color:#FF0000'>Insert info: </span><br/>";
                    for (Chunk chunk : stringComparator.getInsertsFromOriginal()) {
                        diff += StringEscapeUtils.escapeHtml4(chunk.toString()) + "<br/>";
                    }
                }

                if (stringComparator.getDeletesFromOriginal().size() != 0) {
                    diff += "<br/><span style='color:#FF0000'>Delete info: </span><br/>";
                    for (Chunk chunk : stringComparator.getDeletesFromOriginal()) {
                        diff += StringEscapeUtils.escapeHtml4(chunk.toString()) + "<br/>";
                    }
                }

                return contentString + diff + oldValue + newValue;
            }

        } catch (IOException e) {
            LOG.error("compare error", e);

            return "comparator error" + e.toString();
        }
    }
602218
public String generateDiff(GenericObject from, GenericObject to) {
        StringBuilder builder = new StringBuilder();

        builder.append("<table class=\"nomargin diff\">");
        builder.append("<thead>");
        builder.append("<tr>");
        builder.append("<th/>");
        builder.append("<th class=\"texttitle\">From</th>");
        builder.append("<th/>");
        builder.append("<th class=\"texttitle\">To</th>");
        builder.append("</tr>");
        builder.append("</thead>");

        builder.append("<tbody>");

        writeDiff(builder, from, to);

        builder.append("</tbody>");
        builder.append("</table>");

        return builder.toString();
    }
896954
@SuppressWarnings("checkstyle:ReturnCount")
  public static int compareBuff(final DirectBuffer o1, final DirectBuffer o2) {
    requireNonNull(o1);
    requireNonNull(o2);
    if (o1.equals(o2)) {
      return 0;
    }
    final int minLength = Math.min(o1.capacity(), o2.capacity());
    final int minWords = minLength / Long.BYTES;

    for (int i = 0; i < minWords * Long.BYTES; i += Long.BYTES) {
      final long lw = o1.getLong(i, BIG_ENDIAN);
      final long rw = o2.getLong(i, BIG_ENDIAN);
      final int diff = Long.compareUnsigned(lw, rw);
      if (diff != 0) {
        return diff;
      }
    }

    for (int i = minWords * Long.BYTES; i < minLength; i++) {
      final int lw = Byte.toUnsignedInt(o1.getByte(i));
      final int rw = Byte.toUnsignedInt(o2.getByte(i));
      final int result = Integer.compareUnsigned(lw, rw);
      if (result != 0) {
        return result;
      }
    }

    return o1.capacity() - o2.capacity();
  }
636098
public static <T> void dumpUnchanged(Iterable<Difference<T>> diffs) {
		for(Difference<T> diff: diffs) {
			if(Difference.Type.UNCHANGED.equals(diff.getType())) {
				System.out.println(diff);
			}
		}
	}
1086702
function createDifferenceObject( $fromData, $toData )
    {
        $changes = new eZXMLTextDiff();
        $contentINI = eZINI::instance( 'content.ini' );
        $useSimplifiedXML = $contentINI->variable( 'ContentVersionDiffSettings', 'UseSimplifiedXML' );
        $diffSimplifiedXML = ( $useSimplifiedXML == 'enabled' );

        $oldXMLTextObject = $fromData->content();
        $newXMLTextObject = $toData->content();

        $oldXML = $oldXMLTextObject->attribute( 'xml_data' );
        $newXML = $newXMLTextObject->attribute( 'xml_data' );

        $simplifiedXML = new eZSimplifiedXMLEditOutput();

        $domOld = new DOMDocument( '1.0', 'utf-8' );
        $domOld->preserveWhiteSpace = false;
        $domOld->loadXML( $oldXML );

        $domNew = new DOMDocument( '1.0', 'utf-8' );
        $domNew->preserveWhiteSpace = false;
        $domNew->loadXML( $newXML );

        $old = $simplifiedXML->performOutput( $domOld );
        $new = $simplifiedXML->performOutput( $domNew );

        if ( !$diffSimplifiedXML )
        {
            $old = trim( strip_tags( $old ) );
            $new = trim( strip_tags( $new ) );

            $pattern = array( '/[ ][ ]+/',
                              '/ \n( \n)+/',
                              '/^ /m',
                              '/(\n){3,}/' );
            $replace = array( ' ',
                              "\n",
                              '',
                              "\n\n" );

            $old = preg_replace( $pattern, $replace, $old );
            $new = preg_replace( $pattern, $replace, $new );
        }

        $oldArray = explode( "\n", $old );
        $newArray = explode( "\n", $new );

        $oldSums = array();
        foreach( $oldArray as $paragraph )
        {
            $oldSums[] = crc32( $paragraph );
        }

        $newSums = array();
        foreach( $newArray as $paragraph )
        {
            $newSums[] = crc32( $paragraph );
        }

        $textDiffer = new eZDiffTextEngine();

        $pre = $textDiffer->preProcess( $oldSums, $newSums );
        $out = $textDiffer->createOutput( $pre, $oldArray, $newArray );
        $changes->setChanges( $out );
        return $changes;
    }
1139909
public boolean computeDiff(List<JsonDelta> delta, List<String> context, JsonNode node1, JsonNode node2) {
        boolean ret = false;
        if (context.size() == 0 || filter.includeField(context)) {
            JsonComparator cmp = getComparator(context, node1, node2);
            if (cmp != null) {
                ret = cmp.compare(delta, context, node1, node2);
            } else {
                delta.add(new JsonDelta(toString(context), node1, node2));
                ret = true;
            }
        }
        return ret;
    }
1185081
@Override
    public CompletableFuture<Map<String, Change<?>>> diff(Revision from, Revision to, String pathPattern) {
        final ServiceRequestContext ctx = context();
        return CompletableFuture.supplyAsync(() -> {
            requireNonNull(from, "from");
            requireNonNull(to, "to");
            requireNonNull(pathPattern, "pathPattern");

            failFastIfTimedOut(this, logger, ctx, "diff", from, to, pathPattern);

            final RevisionRange range = normalizeNow(from, to).toAscending();
            readLock();
            try (RevWalk rw = new RevWalk(jGitRepository)) {
                final RevTree treeA = rw.parseTree(commitIdDatabase.get(range.from()));
                final RevTree treeB = rw.parseTree(commitIdDatabase.get(range.to()));

                // Compare the two Git trees.
                // Note that we do not cache here because CachingRepository caches the final result already.
                return toChangeMap(blockingCompareTreesUncached(treeA, treeB,
                                                                pathPatternFilterOrTreeFilter(pathPattern)));
            } catch (StorageException e) {
                throw e;
            } catch (Exception e) {
                throw new StorageException("failed to parse two trees: range=" + range, e);
            } finally {
                readUnlock();
            }
        }, repositoryWorker);
    }
1162476
@Override
    public int encode ( byte[] dst, int dstIndex ) {
        if ( this.largeFile ) {
            SMBUtil.writeInt2(this.pid, dst, dstIndex);
            SMBUtil.writeInt4(this.byteOffset >> 32, dst, dstIndex + 4);
            SMBUtil.writeInt4(this.byteOffset & 0xFFFFFFFF, dst, dstIndex + 8);
            SMBUtil.writeInt4(this.lengthInBytes >> 32, dst, dstIndex + 12);
            SMBUtil.writeInt4(this.lengthInBytes & 0xFFFFFFFF, dst, dstIndex + 16);
            return 20;
        }
        SMBUtil.writeInt2(this.pid, dst, dstIndex);
        SMBUtil.writeInt4(this.byteOffset, dst, dstIndex + 2);
        SMBUtil.writeInt4(this.lengthInBytes, dst, dstIndex + 6);
        return 10;
    }
766520
function createUnifiedDiff({ actual, expected }) {
  return diff.createPatch('string', actual, expected)
    .split('\n')
    .splice(4)
    .map(line => {
      if (line.match(/@@/)) {
        return null;
      }
      if (line.match(/\\ No newline/)) {
        return null;
      }
      return line.replace(/^(-|\+)/, '$1 ');
    })
    .filter(line => typeof line !== 'undefined' && line !== null)
    .join('\n');
}
439688
public String diff_text2(LinkedList<Diff> diffs) {
        StringBuilder text = new StringBuilder();
        for (Diff aDiff : diffs) {
            if (aDiff.operation != Operation.DELETE) {
                text.append(aDiff.text);
            }
        }
        return text.toString();
    }
28516
public static boolean isPreviousEncoderSafeOrEqual(Encoder encoderToApply, Encoder previousEncoder) {
        return previousEncoder == encoderToApply || !encoderToApply.isApplyToSafelyEncoded() && previousEncoder.isSafe() && encoderToApply.isSafe()
                || previousEncoder.getCodecIdentifier().isEquivalent(encoderToApply.getCodecIdentifier());
    }