/*
 * 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.util;

public class StructExpressLine {

    public int beginChar;

    public int endChar;

    public String text;

    public String keyword;

    public String number;

    public boolean isStartOfApp;

    public int illegalChars;

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        if (isStartOfApp) 
            res.append("StartOfApp ");
        if (keyword != null) 
            res.append("[").append(keyword).append("] ");
        if (number != null) 
            res.append("N").append(number).append(" ");
        if (illegalChars > 0) 
            res.append("(illegals=").append(illegalChars).append(") ");
        int len = (endChar + 1) - beginChar;
        if (len > 100) 
            len = 100;
        res.append("\"").append(text.substring(beginChar, beginChar + len)).append("\"");
        return res.toString();
    }

    public static StructExpressLine tryParse(String txt, com.pullenti.unisharp.Outargwrapper<Integer> i) {
        for (; i.value < txt.length(); i.value++) {
            if (txt.charAt(i.value) != '\n' && txt.charAt(i.value) != '\r') 
                break;
        }
        if (i.value >= txt.length()) 
            return null;
        StructExpressLine res = new StructExpressLine();
        res.beginChar = (res.endChar = i.value);
        res.text = txt;
        for (; i.value < txt.length(); i.value++) {
            if (txt.charAt(i.value) == '\n' || txt.charAt(i.value) == '\r') 
                break;
            else 
                res.endChar = i.value;
        }
        res._analyze();
        return res;
    }

    private void _analyze() {
        int i;
        for (i = beginChar; i <= endChar; i++) {
            if (!com.pullenti.unisharp.Utils.isWhitespace(text.charAt(i))) 
                break;
        }
        if ((i < endChar) && Character.isUpperCase(text.charAt(i))) {
            for (String kw : m_Keywords) {
                if (_checkWord(text, i, kw)) {
                    keyword = kw;
                    i += kw.length();
                    break;
                }
            }
        }
        for (; i <= endChar; i++) {
            if (!com.pullenti.unisharp.Utils.isWhitespace(text.charAt(i))) 
                break;
        }
        if (i <= endChar) {
            if (text.charAt(i) == 'N' || text.charAt(i) == '№') {
                for (i = i + 1; i <= endChar; i++) {
                    if (!com.pullenti.unisharp.Utils.isWhitespace(text.charAt(i))) 
                        break;
                }
            }
        }
        if (i <= endChar) {
            int j;
            for (j = i; j <= endChar; j++) {
                char ch = text.charAt(j);
                if (Character.isDigit(ch)) 
                    continue;
                if (ch == ')' || ch == '.') 
                    continue;
                if (Character.isLetter(ch)) {
                    if (j > beginChar && Character.isDigit(text.charAt(j - 1))) 
                        continue;
                    if ((j + 1) <= endChar && text.charAt(j + 1) == ')') 
                        continue;
                }
                break;
            }
            if (j > i) {
                number = text.substring(i, i + j - i);
                i = j;
            }
        }
        if (com.pullenti.unisharp.Utils.stringsEq(keyword, "ПРИЛОЖЕНИЕ") || com.pullenti.unisharp.Utils.stringsEq(keyword, "ДОДАТОК")) {
            if (i > endChar) 
                isStartOfApp = true;
        }
        illegalChars = 0;
        for (; i <= endChar; i++) {
            char ch = text.charAt(i);
            if (com.pullenti.unisharp.Utils.isWhitespace(ch) || Character.isLetterOrDigit(ch)) 
                continue;
            if (ch == '_' || ch == '|') {
                illegalChars++;
                continue;
            }
            if (((int)ch) <= 0x80 || ch == '№') 
                continue;
            illegalChars++;
        }
    }

    private static String[] m_Keywords;

    private static boolean _checkWord(String txt, int i, String sub) {
        int j;
        for (j = 0; (j < sub.length()) && ((i + j) < txt.length()); j++) {
            if (Character.toUpperCase(txt.charAt(i + j)) != Character.toUpperCase(sub.charAt(j))) 
                return false;
        }
        if (j >= sub.length()) 
            return true;
        return false;
    }

    public StructExpressLine() {
    }
    
    static {
        m_Keywords = new String[] {"СТАТЬЯ", "СТАТТЯ", "ПРИЛОЖЕНИЕ", "ДОДАТОК"};
    }
}
