/*
 * SDK Pullenti Unitext, version 4.33, fabruary 2026. Copyright (c) 2013-2026, Pullenti. All rights reserved. 
 * Non-Commercial Freeware and Commercial Software. 
 * This class is generated using the converter Unisharping (www.unisharping.ru) from Pullenti C# project. 
 * The latest version of the code is available on the site www.pullenti.ru
 */

package com.pullenti.unitext.internal.word;

// Provides name class for Compound File Storage.
public class ExtendedName {

    private char[] m_Name;

    public char[] getName() {
        return m_Name;
    }


    public ExtendedName(String sname, char[] _name, int offset, int count) throws IllegalArgumentException {
        if (sname != null) 
            this.m_Name = sname.toCharArray();
        else {
            if (_name == null) 
                throw new IllegalArgumentException("name");
            if (count < 0) 
                count = _name.length;
            this.m_Name = new char[count];
            for(int iii = 0; iii < (count); iii++) this.m_Name[(0) + iii] = _name[(offset) + iii];
        }
    }

    @Override
    public String toString() {
        return new String(m_Name);
    }

    public char[] toCharArray() {
        return m_Name.clone();
    }

    public String toEscapedString() {
        StringBuilder sb = new StringBuilder();
        for (char ch : m_Name) {
            if ((ch < ' ') || ch == '\\') 
                sb.append("\\u").append(String.format("%4X", (int)ch));
            else 
                sb.append(ch);
        }
        return sb.toString();
    }

    public static ExtendedName fromString(String _name) throws IllegalArgumentException {
        return new ExtendedName(_name, null, 0, -1);
    }

    public static ExtendedName fromEscapedString(String _name) throws NumberFormatException, IllegalArgumentException {
        StringBuilder sb = new StringBuilder();
        int i = 0;
        while (i < _name.length()) {
            if (_name.charAt(i) != '\\') 
                sb.append(_name.charAt(i));
            else {
                if ((i + 6) > _name.length() || _name.charAt(i + 1) != 'u') 
                    throw new NumberFormatException("Invalid escaped string format");
                short code = com.pullenti.unisharp.Utils.parseShort(_name.substring(i + 2, i + 2 + 4), 0, null);
                sb.append((char)code);
            }
        }
        return new ExtendedName(sb.toString(), null, 0, -1);
    }

    @Override
    public int hashCode() {
        int hash = m_Name.length;
        for (int i = 0; i < m_Name.length; i++) {
            hash ^= ((int)m_Name[i]);
        }
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        return ExtendedName.ooEq(this, (ExtendedName)obj);
    }

    public static boolean ooEq(ExtendedName n1, ExtendedName n2) {
        if (n1.m_Name.length != n2.m_Name.length) 
            return false;
        for (int i = 0; i < n1.m_Name.length; i++) {
            if (n1.m_Name[i] != n2.m_Name[i]) 
                return false;
        }
        return true;
    }

    public static boolean ooNoteq(ExtendedName n1, ExtendedName n2) {
        return !(ExtendedName.ooEq(n1, n2));
    }
    public ExtendedName() {
    }
}
