   :safe #'stringp)
 (make-variable-buffer-local 'clang-format-style)
 
+(defun clang-format--extract (xml-node)
+  "Extract replacements and cursor information from XML-NODE."
+  (unless (and (listp xml-node) (eq (xml-node-name xml-node) 'replacements))
+    (error "Expected <replacements> node"))
+  (let ((nodes (xml-node-children xml-node))
+        replacements
+        cursor)
+    (dolist (node nodes)
+      (when (listp node)
+        (let* ((children (xml-node-children node))
+               (text (car children)))
+          (cl-case (xml-node-name node)
+            ('replacement
+             (let* ((offset (xml-get-attribute-or-nil node 'offset))
+                    (length (xml-get-attribute-or-nil node 'length)))
+               (when (or (null offset) (null length))
+                 (error "<replacement> node does not have offset and length attributes"))
+               (when (cdr children)
+                 (error "More than one child node in <replacement> node"))
+
+               (setq offset (1+ (string-to-number offset)))
+               (setq length (string-to-number length))
+               (push (list offset length text) replacements)))
+            ('cursor
+             (setq cursor (1+ (string-to-number text))))))))
+
+    ;; Sort by decreasing offset, length.
+    (setq replacements (sort (delq nil replacements)
+                             (lambda (a b)
+                               (or (> (car a) (car b))
+                                   (and (= (car a) (car b))
+                                        (> (cadr a) (cadr b)))))))
+
+    (cons replacements cursor)))
+
+(defun clang-format--replace (offset length &optional text)
+  (goto-char offset)
+  (delete-char length)
+  (when text
+    (insert text)))
+
 ;;;###autoload
 (defun clang-format-region (start end &optional style)
   "Use clang-format to format the code between START and END according to STYLE.

;;; clang-format.el --- Format code using clang-format

;; Keywords: tools, c
;; Package-Requires: ((json "1.3"))

;;; Commentary:



;;; Code:

(require 'cl-lib)
(require 'xml)

(defgroup clang-format nil
  "Format code using clang-format."
